# Calculating a quiz score in Javascript - Not working

Hello Everyone!

I’m new to Javascript and taking a course. I would greatly appreciate your help.
My assignment is the following:

• Create a set of if functions that will add one point to the score for each correct answer is correct.
Your if functions must meet the following criteria:

a. They must calculate the correct answer using arithmetic.

b. They must use logical operators to compare the user’s answer to the correct answer (tip: remember that the prompt window will capture your user’s answers as strings).

Here is my setup but I’m not getting the score result? I’m running this in Console.

``````var score = 0;

var name = prompt("Please enter your name.");
var answer1 = prompt("5 * 16");
var answer2 = prompt("17 + 22");
var answer3 = prompt("98 - 33");
var answer4 = prompt("48 / 8");

if (answer1 == 80) {
score = score + 1;
}

if (answer2 == 39) {
score = score + 1;
}

if (answer3 == 65) {
score = score + 1;
}

if (answer4 == 6) {
score = score + 1;
}
``````

Thanks so very much!

If those quotes are actually slanted, your problem is there.
If it’s actual proper quotes and not Mac quotes, then your code works; the `score` variable is holding your answer.

1 Like

Yes!! I changed the quotes to single quotes on the numbers. And the score returned 4! This WORKS! Thank you so very much for your help and quick response! I’m still having some difficulty with Calculating this quiz score.
Would greatly appreciate ANY help or feedback. While I have prompts for each math question, how do I use an IF function to use arithmetic to calculate the correct answer.

I’d like to calculate how many the user got correct out of 10.

Next I will need a logical operator to compare the correct answer to the user’s input.
The correct logical operator should be used to compare the correct answer to the user’s input.

In your original post, you initialize and update the ‘score’ dependent on user responses.
But I don’t see where you do any calculations to display the users correct or incorrect answers.
I also don’t see anything sent to the console as stated,
Plus, your score calculations are done with IF statements, not functions.

1 Like

Thank you jmrker…

I figured out where I went wrong. I

``````if (answer1 == 80) {
score = score + 1;
}

if (answer2 == 39) {
score = score + 1;
}

if (answer3 == 65) {
score = score + 1;
}

if (answer4 == 6) {
score = score + 1;
}

if (answer5 == 198) {
score = score + 1;
}

var result = document.getElementById("result");
var message = document.getElementById("message");
var score = score;

if (name) {
result.innerHTML =
"Hello " + name + "! Your result is " + score + " out of 10.";
}

if (score == 10) {
//If the student achieved a score of 10, the paragraph should read “Congratulations on your perfect score!”
message.innerHTML = "Congratulations on your perfect score!";
}

if (score == 8 || score == 9) {
//If the student achieved a score of 8 or 9, the paragraph should read “You did very well! Keep learning.”
message.innerHTML = "You did very well! Keep learning.";
}
``````

I tested the Quiz and it worked!!

Thank you for your helpful feedback! I have no knowledge of your level or progress in the course, but I would like to recommend the following as your continue.

Return to the project at some future point and attempt to solve using:

1. Two arrays (one for questions and another for answers). Collect the user’s response and store for later comparison to the actual correct answers. An advanced attempt might combine question and responses in a 2-dimension array. (Might be in a follow-up course)
2. Consider using a loop(s) to display and display score and final results.

Just something to consider to keep your interest up as you progress in the course.
Happy coding. Hi @Loranne,

I mentioned in this post Creating a If function to create messages - #5 by rpg_digital that you can format your code quite easily using the </> button in the menu.

I will format your last post, but if in future you could format your code it would be appreciated. It just makes it easier to read 1 Like