Last active
June 2, 2019 20:12
-
-
Save nmanumr/f67014e5f6bd365381233f132d710bb2 to your computer and use it in GitHub Desktop.
Parse wrong questions from quiz details
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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