Skip to content

Instantly share code, notes, and snippets.

@nmanumr
Last active June 2, 2019 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmanumr/f67014e5f6bd365381233f132d710bb2 to your computer and use it in GitHub Desktop.
Save nmanumr/f67014e5f6bd365381233f132d710bb2 to your computer and use it in GitHub Desktop.
Parse wrong questions from quiz details
class TabularParser {
getNthCol(row, n) {
return row.querySelector(`td:nth-child(${n})`);
}
getNthColText(row, n) {
return this.getNthCol(row, n).innerText;
}
parse(doc) {
var data = [];
var rows = doc.querySelectorAll(`.${this.tableContainerClass}>table>tbody>tr`);
rows.forEach((row) => {
if (!row.innerText.match(/No data available/i))
data.push(this.buildObj(row));
})
return data;
}
}
class QuizDetailParser extends TabularParser {
constructor() {
super();
this.tableContainerClass = "allowance_table";
}
buildObj(row) {
return new QuizDetail(
parseInt(this.getNthColText(row, 1)),
this.getNthCol(row, 3).children[0].cloneNode(true),
this.getNthCol(row, 4).children[0].cloneNode(true),
this.getNthCol(row, 5).children[0].cloneNode(true),
)
}
}
class QuizDetail {
constructor(index, question, crctAns, wrngAns) {
this.index = index;
this.text = question;
this.correctAns = crctAns;
this.wrongAns = wrngAns;
}
getJSON() {
return {
text: this.text.innerText.trim(),
correctAns: this.correctAns.innerText.trim(),
wrngAns: this.wrongAns.innerText.trim()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment