I have written this code for calculating the marks in an exam which, of course, includes buttons!!
function wrong_answer() {
var calc = 0;
var calc2 = 1;
var div_ele = document.createElement("div");
div_ele.innerHTML = calc - calc2;
document.getElementById("div1").appendChild(div_ele);
}
function correct_answer() {
var calc = 0;
var calc2 = -3;
var div_ele = document.createElement("div");
div_ele.innerHTML = calc - calc2;
document.getElementById("div2").appendChild(div_ele);
}
.div_wrong {
font-size: larger;
font-family: "Agency FB";
color: red;
font-weight: 700;
}
.div_correct {
font-size: larger;
font-family: "Agency FB";
color: blue;
font-weight: 700;
}
<input id="box" type="hidden" />
<button onclick="wrong_answer()" id="button1">WRONG ANSWER!</button>
<button onclick="correct_answer()">CORRECT ANSWER!</button>
<div id="div1" class="div_wrong"></div>
<div id="div2" class="div_correct"></div>
I want to perform a sum total of the list of numbers thus created by this program. What can I add to my program?
3
Answers
You were declaring calc unneccesarily.
Add a
totalSum
to display the total sum. And to make the calculation a lot easier, changewrong_answer()
toupdateTotal(-1)
andcorrect_answer()
toupdateTotal(3)
. Create the following script-section to calculate thetotalSum
:Your calculation is wrong. It would help if you used a global variable to track the scores. Beyond that, you added unnecessarily repetitive code.
I would advise you to make use of
data
attributes. You can include the score within the data attributes instead of the JS code. This makes the code completely abstractive to which button is clicked.