Konraed
Konraed

Reputation: 13

The score in my JavaScript quiz application it not storing properly. What is wrong with the syntax?

The majority of the code seems to be working. The problem is that it keeps returning the error message that I set in the if statement for calculating the final score message as the final else clause. I'm not sure how to see the value of what is actually being stored in the variable at any given time while the application is running.

var score = 0; // to store the correct answers

//List of answers
var answerOne = 'BLUE';
var answerTwo = 'GREEN';
var answerThree = 'PRINCIPLE';
var answerFour = 'MONEY';
var answerFive = 'WILLY WONKA';

// The questions and their verification protocol

var questionOne = prompt('What is the color of the sky?');
//Conditional statement matching user input to correct answer.  

  if (questionOne.toUpperCase === answerOne) {
    score+= 1;
  }


var questionTwo = prompt('What is the color of grass?');
//Conditional statement matching user input to correct answer.  

  if (questionTwo.toUpperCase === answerTwo) {
      score+= 1;
  }

var questionThree = prompt('What is the most powerful force?');
//Conditional statement matching user input to correct answer. 

  if (questionThree.toUpperCase === answerThree) {
    score+= 1;
  }

var questionFour = prompt('What makes the world go round?');
//Conditional statement matching user input to correct answer.  

 if (questionFour.toUpperCase === answerFour) {
    score+= 1;
  }

var questionFive = prompt('Who can take a sunrise and sprinkle it with dew?');
//Conditional statement matching user input to correct answer. 

  if (questionFive.toUpperCase === answerFive) {
    score+= 1;
  }

//Final score-providing message to user

  if (score = 0) {
    alert('Wow, you suck. No crown for you! You had ' + score + 'correct answers!');
  } else if (score <= 2 && score > 1) {
      alert('You were not the worst, but certainly not the best. You earned a bronze crown with ' + score + ' correct answers!');
    } else if (score <= 4 && score > 3) {
        alert('You did a pretty good job! You earned a silver crown with ' + score + ' correct answers!');
    } else if (score === 5) {
        alert('Congratulations! You have successfully answered all questions correctly! You have earned a gold crown with ' + score + ' correct answers!');
    } else {
      alert('ERROR!')
    }

Upvotes: 1

Views: 56

Answers (3)

Rob Welan
Rob Welan

Reputation: 2210

toUpperCase is a method and, as such, should be written per this example:

questionOne.toUpperCase()

Upvotes: 1

Andy
Andy

Reputation: 63589

There are a number of issues with this code.

1) toUpperCase is a string function and should be used like:

if (questionFour.toUpperCase() === answerFour) {...

2) In your if/then statement your are assigning 0 to score, not checking that score equals 0. To do that:

if (score === 0) {

3) Finally you need to watch your if/then condition ranges.

This doesn't check if score is 1:

(score <= 2 && score > 1)

This does check if score is 1:

(score >= 1 && score <= 2)

Here's the corrected code.

Upvotes: 1

azerty
azerty

Reputation: 33

you used assignment operator not the ==.

if(score==0)

to see the value of score add console.log(score) above that.

Upvotes: 0

Related Questions