コードの大部分が機能しているようです。問題は、最後のスコアメッセージを最終的なelse節として計算するためにif文で設定したエラーメッセージを返すことです。アプリケーションの実行中にいつでも変数に実際に格納されている値を確認する方法がわかりません。私の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)'(私は常に厳密な等式を使います)でなければなりません。あなたのコードでは、スコアを0に設定しています)。 – Andy
toUpperCaseはメソッドであるため、questionOne.toUpperCase()など( –
)@RobWelanと答えてください。 – zfrisch