var first = prompt("It is a great time outside, isn't it? (yes / no)");
first = "yes";
if (first) {
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,b,c) {
alert("Perfect! You've answered 3 questions correct"); <!--i allways get this one, no matter if its all wrong... -->
} 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");
}
那么什么是正确的代码?记住它必须是初级水平,我正在训练基础知识。提前谢谢
答案 0 :(得分:0)
我认为这是一个简单的错误,即你的第一个条件不符合你的想法。
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?");
否则,无论提示返回什么,都会覆盖该值。
答案 1 :(得分:0)
您没有正确测试答案,您应该在一个语句中阅读用户答案,然后在另一个语句中使用它进行测试
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("It is a great time outside, isn't it? (yes / no)");
if (first=="yes") {
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?");
var b = prompt("How many legs has a human body?");
var c = prompt("How many planets are there in the Solar system?");
var correctanswerscount = 0;
if (a==1)
correctanswerscount++;
if (b==2)
correctanswerscount++;
if (c==9)
correctanswerscount++;
if(correctanswerscount == 3) {
alert ("Perfect! You've answered 3 questions correct");
} else if (correctanswerscount == 0) {
alert("Sorry, you did not answer any question correct");
} else if (correctanswerscount == 1) {
alert("Bad! you gave only 1 correct answer");
} else if (correctanswerscount == 2) {
alert("Not bad! You've answered 2 questions correct");
}
答案 2 :(得分:0)
收集用户的回答后,您立即将答案设置回正确的答案:(注释所有三个提示的第二行)
var a = +prompt("How many trunks does an elephant have?");
//a = 1;
接下来,您的初始比较只会检查是否存在值,而不是它们是否正确
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");
}