Javascript 我想向用户展示他回答了多少正确的问题。但它总是显示带有3个正确ANWSER的ffirst警报

Javascript 我想向用户展示他回答了多少正确的问题。但它总是显示带有3个正确ANWSER的ffirst警报,javascript,html,Javascript,Html,var first=prompt(“这是户外的好时光,不是吗?(是/否)”); first=“yes”; 如果(第一){ 警惕(“我同意你,酷!现在让我们看看你是否能完成下一个任务”); }否则{ 警惕(“对不起,你错了”); } var a=+prompt(“一头大象有多少树干?”); a=1; var b=+提示符(“人体有多少条腿?”); b=2; var c=+prompt(“太阳系中有多少行星?”); c=9; 如果(a、b、c){ 警惕(“完美!你回答了3个正确的问题”); }否则如

var first=prompt(“这是户外的好时光,不是吗?(是/否)”);
first=“yes”;
如果(第一){
警惕(“我同意你,酷!现在让我们看看你是否能完成下一个任务”);
}否则{
警惕(“对不起,你错了”);
}
var a=+prompt(“一头大象有多少树干?”);
a=1;
var b=+提示符(“人体有多少条腿?”);
b=2;
var c=+prompt(“太阳系中有多少行星?”);
c=9;
如果(a、b、c){
警惕(“完美!你回答了3个正确的问题”);
}否则如果(a!==1&&b!==2&&c!==9){
警告(“对不起,您没有正确回答任何问题”);
}如果(a==1&&b!==2&&c!==9 | | a!==1&&b==2&&c!==9 | | a!==1&&b!==2&&c==9){
警告(“糟糕!你只给出了一个正确答案”);
}如果(a==1&&b==2&&c!==9 | | a!==1&&b==2&&c==9 | | a==1&&b!==2&&c==9){
警惕(“不错!你回答了两个正确的问题”);

}
我认为这是一个简单的错误,即您的第一个条件没有按照您认为的那样执行

   if(a == 1 && b == 2 && c == 9)
是你的第一个条件应该是什么样子

此外,在上面的行中,不应将a、b和c设置为这些值

    var a = +prompt("How many trunks does an elephant have?");
    a = 1;
    var b = +prompt("How many legs has a human body?");
    b = 2;
    var c = +prompt("How many planets are there in the Solar system?");
    c = 9;
应该是

    var a = +prompt("How many trunks does an elephant have?");
    var b = +prompt("How many legs has a human body?");
    var c = +prompt("How many planets are there in the Solar system?");

否则,无论提示返回什么,您都会覆盖该值。

您没有正确测试答案。您应该在一条语句中读取用户答案,然后在另一条语句中使用它进行测试

 var a = +prompt("How many trunks does an elephant have?");//reading the user input
 if(a==1)//testing the if user input is equals to the correct answer
 {...}
var first=prompt(“这是户外的好时光,不是吗?(是/否)”);
如果(第一个=“是”){
警惕(“我同意你,酷!现在让我们看看你是否能完成下一个任务”);
}否则{
警惕(“对不起,你错了”);
}
var a=提示(“一头大象有多少鼻子?”);
var b=提示(“人体有多少条腿?”);
var c=提示(“太阳系中有多少行星?”);
var CorrectAnswerScont=0;
如果(a==1)
正确答案NT++;
如果(b==2)
正确答案NT++;
如果(c==9)
正确答案NT++;
如果(CorrectAnswerScont==3){
警惕(“完美!你回答了3个正确的问题”);
}否则如果(CorrectAnswerScont==0){
警告(“对不起,您没有正确回答任何问题”);
}否则如果(correctanswerscount==1){
警告(“糟糕!你只给出了一个正确答案”);
}否则如果(CorrectAnswerScont==2){
警惕(“不错!你回答了两个正确的问题”);

}
从用户处收集答案后,您将立即将答案设置回正确答案:(注释掉第二行的所有三个提示)

接下来,您的初始比较只是检查是否存在值,而不是它们是否正确

if (a,b,c) {
改为:

if (a===1 && b===2 && c===9) {
所以你要检查每一个答案是否正确。“==”运算符检查相等性和变量类型

完整代码:

var first = prompt("It is a great time outside, isn't it? (yes / no)");
//first = "yes";

if (first.toLowerCase().startsWith("y")) {
    alert("I do agree with you, cool! Now let's see if you can complete next task");
} else {
    alert("Sorry but you are wrong");
}

var a = +prompt("How many trunks does an elephant have?");
//a = 1;
var b = +prompt("How many legs has a human body?");
//b = 2;
var c = +prompt("How many planets are there in the Solar system?");
//c = 9;

if (a===1 && b===2 && c===9) {
    alert("Perfect! You've answered 3 questions correct"); 
} else if (a !== 1 && b !== 2 && c !== 9) {
    alert("Sorry, you did not answer any question correct");
} else if (a == 1 && b !== 2 && c !== 9 || a !== 1 && b == 2 && c !== 9 || a !== 1 && b !== 2 && c == 9) {
    alert("Bad! you gave only 1 correct answer");
} else if (a == 1 && b == 2 && c !== 9 || a !== 1 && b == 2 && c == 9 || a == 1 && b !== 2 && c == 9) {
    alert("Not bad! You've answered 2 questions correct");
}

谢谢。。。非常简单,我的意思是像我这样的初学者可以理解。再一次非常感谢。很有趣,最类似于我的,但也有一个小问题,也许你没有注意到,无论我在关于天气的第一个问题中回答什么,我都会得到第一个警报。。。即使它是错误的,应该提醒对不起布拉布拉布拉布拉布拉布拉。。。无论如何,谢谢您无论用户输入什么,您都明确地将第一个变量设置为“yes”。然后,比较在应该比较字符串时查找布尔值。请参阅我编辑的答案。
var first = prompt("It is a great time outside, isn't it? (yes / no)");
//first = "yes";

if (first.toLowerCase().startsWith("y")) {
    alert("I do agree with you, cool! Now let's see if you can complete next task");
} else {
    alert("Sorry but you are wrong");
}

var a = +prompt("How many trunks does an elephant have?");
//a = 1;
var b = +prompt("How many legs has a human body?");
//b = 2;
var c = +prompt("How many planets are there in the Solar system?");
//c = 9;

if (a===1 && b===2 && c===9) {
    alert("Perfect! You've answered 3 questions correct"); 
} else if (a !== 1 && b !== 2 && c !== 9) {
    alert("Sorry, you did not answer any question correct");
} else if (a == 1 && b !== 2 && c !== 9 || a !== 1 && b == 2 && c !== 9 || a !== 1 && b !== 2 && c == 9) {
    alert("Bad! you gave only 1 correct answer");
} else if (a == 1 && b == 2 && c !== 9 || a !== 1 && b == 2 && c == 9 || a == 1 && b !== 2 && c == 9) {
    alert("Not bad! You've answered 2 questions correct");
}