class MCQ{ /* Declcaring a class called MCQ */
constructor(question, choice, answer) {
this.question = question;
this.choice = choice;
this.answer = answer;
}
}
var qn = [];
qn[0] = new MCQ(["Question 0",["Answer 1", "Answer 2", "Answer 3", "Answer 4"],1]); // 1st element is question, 2nd is choices, 3rd is correct answer
qn[1] = new MCQ(["Question 1",["Answer 1", "Answer 2", "Answer 3", "Answer 4"],1]); // 1st element is question, 2nd is choices, 3rd is correct answer
qn[2] = new MCQ(["Question 2",["Answer 1", "Answer 2", "Answer 3", "Answer 4"],2]); // 1st element is question, 2nd is choices, 3rd is correct answer
qn[3] = new MCQ(["Question 3",["Answer 1", "Answer 2", "Answer 3", "Answer 4"],3]); // 1st element is question, 2nd is choices, 3rd is correct answer
function display () {
for (i=0; i < (qn.length); i++) {
console.log(qn[i].question);
var getans = input.question("Please enter choice >> ")
if (getans == qn[i].answer) {
console.log("You are correct!");
} else {
console.log ("You are wrong!");
}
}
}
function displayHTML () {
// display all questions in HTML
}
// main
var input = require('readline-sync');
console.log("Running main program\n");
display();
I think you are not correctly passing arguments. What you are doing is just passing 1 whole array so that is one argument, not 3.
What would be correct is to do this.
Another question. How do i display the following data in the function displayHTML function.
I like to get the question from array
Display the choices as radio button
Allow user select
Allow user submit
And check if answer is correct
An example of the html linking the javascript would be helpful for me to understand
My html would be index.html… and my javascript would be quiz.js
<!DOCTYPE html>
<html>
<head>
<title>
Simple Javascript Quiz Demo
</title>
<!-- [CSS AND JS] -->
<link href="quiz-css.css" rel="stylesheet">
<script src="quiz.js"></script>
</head>
<body>
<p> ......an example will help me understand how I get data from javascript and post it back </p>
</body>
</html>
Hi @Ronald.
You need to manage the html through JavaScript.
You can’t call the JavaScript code like that in the html, it will be evaluated as text.
I suggest you to do all the JavaScript and the jQuery modules first and than try it again.
Those modules would be sufficient to answer the question. After those, I advise you to take a look also at React which will provide you with a way to mix html and js code.
In the browser, JavaScript is loaded after HTML. There are multiple ways to include JavaScript in HTML.
You can add at the end of body tag. You can also add it through the external file.
How you invoke JavaScript is through function calls on Event. You can read about events here.
So for example, a page is loaded, a button is clicked or a radio button is checked/unchecked then the JavaScript you have attached to the event will be automatically invoked and perform the action you have asked him to do.
I hope this all makes sense.