Quiz score calculation

Hi. I’ve got most of the code working here:

e-learning example (codepen.io)

But I think there is something wrong with this line as it currently doesn’t work. Any help would be appreciated.

 }   if (checkValue == true && radioBtn.value == true) {
       num_correct++;```

How can you tell it isn’t working? I can’t even get it running.

I think there is a syntax problem with your code, but I would have a hard time diagnosing it because the formatting is so erratic. Formatting is your friend. Yes, it’s a pain until you get used to it and it becomes instinctual, but it will save you soooooooo much hassle in the long run. There have been sooooooo times when I have spotted problems just because the formatting is off.

CP even has a built in fomatter, in the pulldown menu each code pane - use it. But it won’t work until you fix the syntax errors - it can’t format code if it can’t tell what the structure is supposed to be. Also, in JS we don’t name variables in snake_case, use use camelCase traditionally, for most things.

Hi Kevin. I never knew there was a formatter thats great thanks, I’ve been spending a long time on that previously to make sure its all good.

I think the code needed an extra function, I took one out because I thought I could do it all in the same function but apparently not. Still has some issues though but I think its slightly closer

e-learning example (codepen.io)

I would keep the browser’s dev tools console open when you click the Submit button.

I have no idea what any of that means, except the last one thats telling me the function isnt defined in the html button… but it is so…??

In addition to CP’s JS formater, I think you need to run the JS code analyzer as well. Yes, you have started to define a checks function but where exactly does it end?

this is the problem. It needs to:
1: check each radioset is checked and if not display a message to select an answer

2:If all radiosets have been checked to calculate a score

The problem is I can’t figure out where to initialise the second function

Maybe the hints I’m giving you aren’t good enough, so I’ll try to be more direct. You start defining the checks function on line three. Notice that the letter ‘f’ in function is at column 0 on the line (i.e. right at the beginning of the line). You know that this function definition will be wrapped in curly braces. Since you formatted your JS nicely now, you know that the closing curly brace for this function should also be at the very beginning of a line. Scroll down until you reach the next function (checkedAll). Do you see a corresponding closing curly brace for the checks function at the beginning of a line before you reach the checkedAll function?

Okay so the syntax is sorted I think. Now I’m getting the error that this is not function at line 18:

const radioBtns = radioSets.querySelectorAll('input[type=radio]');

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.