i am making a quiz and i want to find out which button has the most clicks so i can give the answer
i have these variables in the start set to zero and their scores will increase by user input
var earthScore = 0; var venusScore = 0;
whenever the button is clicked a fuction runs
q1a4.addEventListener ("click", earth); q1a5.addEventListener ("click", venus);
this function will add one to the original variables
function earth () { earthScore += 1; questionCount += 1; }
function venus () { venusScore += 1; questionCount += 1; }
now i want to find out the variable which has the most clicks and give an answer to user so for example; if earth variable has greater score than venus variable then i will change the result to result.innerHTML = 'you are earth'
but i don’t know how to find the variable with the greatest score
this is actually my first project using js so i dont have that much idea and everything i tried to find seems wrong so i would really appreciate if anyone can give me an answer for my program.
2
Answers
You might put the Results into an Array afterwards with:
Now, you have the highest value in:
Refer to: https://www.w3schools.com/js/js_array_sort.asp
Take a look at the solution first:
Explanation:
handleResult
which will update the result with the current winner (or nothing when we have a tie)earth
andvenus
like you initially defined, except for callinghandleResult
as well to handle the resultclick
events for the buttons like you specified