gpt4 book ai didi

javascript - 我的 JavaScript 测验应用程序中的分数没有正确存储。语法有什么问题?

转载 作者:行者123 更新时间:2023-11-30 15:07:51 25 4
gpt4 key购买 nike

大部分代码似乎都有效。问题是它不断返回我在 if 语句中设置的用于计算最终分数消息的错误消息作为最终的 else 子句。我不确定如何在应用程序运行时的任何给定时间查看实际存储在变量中的值。

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

最佳答案

这段代码有很多问题。

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 .

关于javascript - 我的 JavaScript 测验应用程序中的分数没有正确存储。语法有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45495074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com