2017-08-03 18 views
1

大部分代碼似乎都在工作。問題在於,它一直返回我在if語句中設置的錯誤消息,以計算最終得分消息作爲最終else子句。我不確定如何在應用程序運行時的任何給定時間查看實際存儲在變量中的值。我的JavaScript測驗應用程序中的分數沒有正確存儲。語法有什麼問題?

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!') 
    } 
+3

那麼,在這裏你可以修正錯誤:'if(score = 0)'。它應該是'if(score === 0)'(我總是使用嚴格的平等)。在你的代碼中,你是_assigning_ 0得分)。 – Andy

+2

toUpperCase是一種方法,因此應該寫成questionOne.toUpperCase()(等) –

+0

@RobWelan你應該提交作爲答案 – zfrisch

回答

1

有許多的與此代碼的問題。

1)toUpperCase是一個字符串函數,應該像使用:

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

2)在你的if/then聲明你是分配0score,不檢查score等於0。要做到這一點:

if (score === 0) { 

3)最後,你需要看你的if/then條件範圍。

檢查score爲1:

(score <= 2 && score > 1) 

確實檢查,如果比分是1:

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

這裏的corrected code

0

您使用了賦值運算符而不是==。

if(score==0) 

看到分數值的增加console.log(score)以上。

+2

...這實際上並沒有回答這個問題 – zfrisch

1

toUpperCase是一種方法,並且這樣,應按照本例被寫成:

questionOne.toUpperCase()