大部分代碼似乎都在工作。問題在於,它一直返回我在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!')
}
那麼,在這裏你可以修正錯誤:'if(score = 0)'。它應該是'if(score === 0)'(我總是使用嚴格的平等)。在你的代碼中,你是_assigning_ 0得分)。 – Andy
toUpperCase是一種方法,因此應該寫成questionOne.toUpperCase()(等) –
@RobWelan你應該提交作爲答案 – zfrisch