0

I would like to create a simple tally system to record the data-id from the elements that are selected and combine the data-id values at the end to generate a result.

function myAnswer() {
  document.getElementById('btnNxt').removeAttribute('disabled');
  var myResult = '';
  var iId = this.getAttribute('data-id');
  myObj[page].mySel = iId;
  myQueRep[page] = iId;
  console.log(iId);
  for (var x = 0; x < btn.length; x++) {
    if (iId == x) {
      btn[x].classList.add('selAnswer');
    } else {
      btn[x].classList.remove('selAnswer');
    }
 }

}

In this section, the iId variable gets the data-id value but I'm not sure how to tally the selections up and display a result based on that score.

JSFiddle of current code:

https://jsfiddle.net/mkykmg15/2/

1
  • If you truly want a "system" then make some fundamental quiz objects. Existing code will get simpler because it will be using objects and their methods, not futzing with every individual variable in nested scopes. Tallying will be a consequence: Player.score might be as simple as return Quiz.score(Player.answers). I see these objects: Question, Quiz (a collection of questions), Player, Page (maybe). Questions may need unique IDs for easy referencing, answer checking, etc. That might get rid of the page counting. Commented Apr 14, 2018 at 18:18

1 Answer 1

1

You should be doing something with your myQueRep.

So something like:

var myQueRep = ["1", "1", "1", "2", "2", "2"]

var tally = myQueRep.reduce( function (acc, curr) {
  return acc + +curr
}, 0);

console.log(tally)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.