-1

I am new to HTML and JavaScript and I am trying to write the function solution that will return a integer score verifying how many check-boxes selected by the user are correct. The function should_be_checked(question_index, choice_index) is already defined which returns true if the answer is correct or else returns false. I want to use the function should_be_checked inside my solution function to evaluate the score. How do I get the input values in an array from the HTML form and then generate the score using the array values against the correct values from the should_be_checked function. For example: If i do should_be_checked(2, 2) it will return true as it is the right answer. question_index and choice_index are the values passed as argument to should_be_checked function. The solution() function will be called on a button click once the user selects the check-boxes.

function solution() {
  var score = 0;
  $("input[type=checkbox]").each(checkbox => {
      let args = checkbox.attr("id").split("-"); // Split at the dashes to get an array containing ["choice", "1", "2"]
      args.shift() // Remove the first element that says "choice"
      if (checkbox.prop("checked") !== should_be_checked(...args)) {
        score = score + 1; // Increment the score
      });
    return score;
  }
<form>
  <fieldset id="question-1">
    <legend>Easy question</legend>
    <input type="checkbox" id="choice-1-1" checked>Incorrect answer</input>
  </fieldset>
  <fieldset id="question-2">
    <legend>Another sample question</legend>
    <input type="checkbox" id="choice-2-1">Sample incorrect answer</input><br>
    <input type="checkbox" id="choice-2-2" checked="checked">Sample correct answer</input>
  </fieldset>
</form>

14
  • Are you using jQuery or something like that? Commented May 4, 2018 at 18:06
  • 1
    I don't see any reference to a should_be_checked function in the code provided here -- just an empty solution function. Commented May 4, 2018 at 18:07
  • jQuery is supported and I can use it. Commented May 4, 2018 at 18:07
  • What is your should_be_checked function and how do you use it? Commented May 4, 2018 at 18:08
  • @MikeMcCaughan yeah right, I am not sure how do I use it in my function. I am a newbie to JavaScript. Commented May 4, 2018 at 18:08

1 Answer 1

2

If you loop over each checkbox you can check if they should be checked using your function

let allQuestionsCorrect = true;
$("input[type=checkbox]").each(checkbox => {
  let args = checkbox.attr("id").split("-"); // Split at the dashes to get an array containing ["choice", "1", "2"]
  args.shift() // Remove the first element that says "choice"
  if(checkbox.prop("checked") !== should_be_checked(...args)) {
    allQuestionsCorrect = false; // If it shouldn't be checked and it is or should be checked and it isn't, the questions are not all correct
  }
});
return allQuestionsCorrect

Then if their checked property isn't what it should be return false

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

5 Comments

Thanks a lot! This is what i wanted to do, I just was not able to loop over the input check boxes as I am a newbie to JavaScript and it's syntax.
@Dookoto_Sea This is a nice jquery feature, you can loop over things the same way you use CSS selectors. input[type=checkbox] in css will apply a style to all checkboxes, jquery will return a list of all the checkboxes
Does the (...args) pass the arguments as 1,2? I thought args is an array here
@Dookoto_Sea Yes, in javascript you can use ... infront of an array in arguments to pass them as seperate arguments. This would be the same as should_be_checked(args[0], args[1])
Okay, this solves my problem and helps me to understand how things work here.

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.