我的JavaScript测验应用程序中的分数没有正确存储。语法有什么问题?
大部分代码似乎都在工作。问题是它一直返回我在if语句中设置的错误消息,用于将最终分数消息作为最终else子句计算。我不确定在应用程序运行时,如何查看在任何给定时间实际存储在变量中的内容的值我的JavaScript测验应用程序中的分数没有正确存储。语法有什么问题?,javascript,Javascript,大部分代码似乎都在工作。问题是它一直返回我在if语句中设置的错误消息,用于将最终分数消息作为最终else子句计算。我不确定在应用程序运行时,如何查看在任何给定时间实际存储在变量中的内容的值 var score = 0; // to store the correct answers //List of answers var answerOne = 'BLUE'; var answerTwo = 'GREEN'; var answerThree = 'PRINCIPLE'; var answe
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!')
}
var评分=0;//存储正确的答案
//答案清单
var answerOne=‘蓝色’;
var answerTwo=‘绿色’;
var answerThree=‘原则’;
var answerFour=‘金钱’;
var answerFive=‘WILLY WONKA’;
//问题及其验证协议
var questionOne=prompt(‘天空是什么颜色?’);
//将用户输入与正确答案匹配的条件语句。
如果(questionOne.toUpperCase===answerOne){
分数+=1;
}
var questionTwo=prompt(‘草是什么颜色?’);
//将用户输入与正确答案匹配的条件语句。
如果(问题2.toUpperCase==答案2){
分数+=1;
}
var questionThree=prompt(‘什么是最强大的力量?’);
//将用户输入与正确答案匹配的条件语句。
如果(问题3.toUpperCase==答案3){
分数+=1;
}
var questionFour=prompt(‘什么使世界运转?’);
//将用户输入与正确答案匹配的条件语句。
如果(问题4.toUpperCase==答案4){
分数+=1;
}
var questionFive=prompt(‘谁能把日出洒上露水?’);
//将用户输入与正确答案匹配的条件语句。
如果(问题5.toUpperCase==回答5){
分数+=1;
}
//向用户提供消息的最终分数
如果(分数=0){
警惕(“哇,你真烂。你没有王冠!你有'+score+'正确答案!');
}否则如果(得分1){
警惕(“你不是最差的,但肯定不是最好的。你以“+score+”正确答案”赢得了铜牌冠军!”);
}否则如果(得分3){
警惕(“你做得很好!你以“+score+”正确答案”赢得了银冠!”;
}否则如果(分数==5){
警报('祝贺您!您成功地正确回答了所有问题!您以'+score+'正确答案!''赢得了金冠!');
}否则{
警报('错误!')
}
您使用的赋值运算符不是==
if(score==0)
要查看score的值,请在其上方添加
console.log(score)
。toUpperCase
是一种方法,因此应根据此示例编写:
questionOne.toUpperCase()
此代码存在许多问题 1)
toUpperCase
是一个字符串函数,应该像以下那样使用:
if (questionFour.toUpperCase() === answerFour) {...
2) 在if/then
语句中,您将0
赋值给score
,而不是检查score
是否等于0
。为此:
if (score === 0) {
3) 最后,您需要观察您的if/then
条件范围
这不会检查分数是否为1:
(score <= 2 && score > 1)
(分数1)
这会检查分数是否为1:
(score >= 1 && score <= 2)
(score>=1&&score好吧,这里有一个错误,您可以修复:if(score=0)
。它应该是if(score==0)
(我总是使用严格的等式)。在您的代码中,您将0分配给score)。toUpperCase是一个方法,因此应该编写为questionOne.toUpperCase()(等等)@RobWelan您应该提交回答您还需要查看您的if/then
声明。例如:(分数1)
。如果你的分数是1,它将返回一个错误…这实际上并不能回答问题