2016-10-04 42 views
0

我想要做一个简单的测验,它应该总结正确的答案和不正确的答案。事情是,虽然我把三个正确的答案中的两个,我仍然得到正确和不正确的数组相同的结果:0.因此,在评估函数中,最后肯定会出现错误。在此先感谢Javascript编码测验不加起来

var responsesArray= []; 
 
var correct=[]; 
 
var incorrect= []; 
 

 

 

 

 
function question2() { 
 

 
    var firstQuestion = prompt('Does null === 0 ? (Yes or No)') 
 

 
// why do you need to convert the answer to lowercase? 
 
    if (firstQuestion.toLowerCase() === 'yes') { 
 
    firstQuestion = true 
 
    } else if (firstQuestion.toLowerCase() === 'no') { 
 
    firstQuestion = false 
 
    } else { 
 
// what if the user writes something other than yes or no? 
 
// they will have to answer the question again 
 
    alert("Please answer either Yes or No"); 
 
    return question2(); 
 
    } 
 
    responsesArray.push(firstQuestion); // add the true or false value to the responses array 
 
} 
 

 
question2(); 
 

 

 

 
function question3() { 
 
    var js = prompt('What was the original name for JavaScript: Java, LiveScript, JavaLive, or ScriptyScript?'); 
 
    js = js.toLowerCase(); 
 
    switch (js) { 
 
    // your own answers 
 
    
 
    case "livescript": 
 
    console.log("Correct!"); 
 
    break; 
 
    case "Java": 
 
    console.log("wrong"); 
 
    break; 
 
    case "JavaLive": 
 
    console.log("wrong"); 
 
    break; 
 
    case "ScriptyScript": 
 
    console.log("wrong"); 
 
    break; 
 
    default: 
 
    console.log("Sorry the answer is LiveScript"); 
 
    } 
 
    responsesArray.push(js); 
 
    
 
    var mine = prompt('What coding language is exclusively related to the back-end: Ruby, JavaScript, HTML?'); 
 
    mine= mine.toLowerCase(); 
 
    switch (mine) { 
 
    // your own answers 
 
    \t case "ruby": 
 
    console.log("Yeah!"); 
 
    break; 
 
    case "html": 
 
    console.log("ouuu I'm sorry for you"); 
 
    break; 
 
    case "javascript": 
 
    console.log("Yeah but so so"); 
 
    break; 
 
    } 
 
    responsesArray.push(mine); 
 
} 
 

 
question3(); 
 

 
function evaluate(responsesArray) 
 
{ 
 

 
for (var i = 0; i < responsesArray.length; i++) 
 
\t { 
 
\t \t if (responsesArray[i] === true|| "livescript" || "ruby") 
 
    { 
 
     correct++; 
 

 
    } else{ 
 
     if (responsesArray[i] !== true|| "livescript" || "ruby") { 
 
     incorrect++; 
 
    } 
 
\t } 
 
}

+0

您有很多语法错误。运行它并看看。 –

+2

@PraveenKumar,没有其他语法错误,比一个失踪的右大括号... – trincot

+0

@trincot嗯,是的。 –

回答

1

定义的数组存储了正确的答案,然后比较正确和用户的反应,并且很容易确定其是否正确。

请检查下面的代码段。

var responsesArray= []; 
 
var correct=0; 
 
var incorrect= 0; 
 
//Correct answer key initialize 
 
var index = 0; 
 
//Initialize array to store correct answer. 
 
var correctAnswers = []; 
 

 

 
function question2() { 
 
    //Save correct answer. 
 
    correctAnswers[index++] = "yes"; 
 
    var firstQuestion = prompt('Does null === 0 ? (Yes or No)') 
 

 
    // why do you need to convert the answer to lowercase? 
 
    if (firstQuestion.toLowerCase() === 'yes') { 
 
    console.log("correct"); 
 
    firstQuestion = 'yes' 
 
    } else if (firstQuestion.toLowerCase() === 'no') { 
 
    console.log("in-correct"); 
 
    firstQuestion = 'no' 
 
    } else { 
 
    // what if the user writes something other than yes or no? 
 
    // they will have to answer the question again 
 
    alert("Please answer either Yes or No"); 
 
    return question2(); 
 
    } 
 
    responsesArray.push(firstQuestion); // add the true or false value to the responses array 
 
} 
 

 
question2(); 
 

 

 

 
function question3() { 
 
    //Save correct answer. 
 
    correctAnswers[index++] = "livescript"; 
 
    var js = prompt('What was the original name for JavaScript: Java, LiveScript, JavaLive, or ScriptyScript?'); 
 
    js = js.toLowerCase(); 
 
    switch (js) {  
 
     // your own answers 
 

 
    case "livescript": 
 
     console.log("Correct!"); 
 
     break; 
 
    case "Java": 
 
     console.log("wrong"); 
 
     break; 
 
    case "JavaLive": 
 
     console.log("wrong"); 
 
     break; 
 
    case "ScriptyScript": 
 
     console.log("wrong"); 
 
     break; 
 
    default: 
 
     console.log("Sorry the answer is LiveScript"); 
 
    } 
 
    responsesArray.push(js); 
 
    
 
    //Save correct answer. 
 
    correctAnswers[index++] = "ruby"; 
 
    var mine = prompt('What coding language is exclusively related to the back-end: Ruby, JavaScript, HTML?'); 
 
    mine= mine.toLowerCase(); 
 
    switch (mine) { 
 
     // your own answers 
 
    case "ruby": 
 
     console.log("Yeah!"); 
 
     break; 
 
    case "html": 
 
     console.log("ouuu I'm sorry for you"); 
 
     break; 
 
    case "javascript": 
 
     console.log("Yeah but so so"); 
 
     break; 
 
    } 
 
    responsesArray.push(mine); 
 
    //Call function to evaluate correct or incorrect answer 
 
    evaluate(responsesArray,correctAnswers) 
 
} 
 

 
question3(); 
 

 
function evaluate(responsesArray,correctAnswers) 
 
{ 
 
    for (var i = 0; i < responsesArray.length; i++) 
 
    { 
 
    //Match response with correct answer. 
 
    if (responsesArray[i] === correctAnswers[i]) 
 
    { 
 
     correct++; 
 

 
    } else{ 
 
     if (responsesArray[i] !== correctAnswers[i]) { 
 
     incorrect++; 
 
     } 
 
    } 
 
    } 
 
    alert("Correct : "+correct+" and Incorrect : "+incorrect); 
 
}

+0

仍然不能正常工作,相同的结果正确=> 0 – Defoe

+0

您能否在您的问题中以工作模式更新代码段?所以我可以在其中进行调试并可以帮助您。 –

+1

我看到你已经重用了'correctAnswers'变量,我已经在我的答案中提出了。对你有好处;-) – trincot

0

您测试正确答案的方式是错误的。相反,用正确的答案定义数组,然后确认如下:

var correct = incorrect = 0; // better initialise your variables 

function evaluate(responsesArray) { 
    var correctAnswers = [true,"livescript","ruby"]; 
    for (var i = 0; i < responsesArray.length; i++) { 
    if (responsesArray[i] === correctAnswers[i]) { 
     correct++; 
    } else { 
     incorrect++; 
    } 
    } 
} 

你有什么:

if (responsesArray[i] === true|| "livescript" || "ruby"){ 

但是,这意味着:

if the answer was true, or .... "livescript" is true, or ... "ruby" is true, then 

如JavaScript认为字符串是truthyif条件将始终为真。

还要注意有没有必要做第二if,因为如果第一if条件是假的,这意味着你已经筛选那些答案是错的情况下,else部分时,才会执行。

最后,你的计数器变量应该在开始递增之前定义。它的工作原理没有这个定义,但是如果两个变量中的一个没有增加,在你致电evaluate后,它仍然是未定义的。最好总是定义你的变量。

+0

不,您的解决方案只是将正确的答案存储在正确的变量中的correctAnswers变量中。 – Defoe

+0

我想你误会了。我只是将错误的'if'代码翻译成了正确的语法,将正确的值不放在'if'中,而是放在一个变量中。但是我看到你更喜欢Rahul的回答,他实际上抄袭了我的想法。但没有问题;-) – trincot

+0

是的你是对的,但Rahul增加了另外50%使它工作(索引++,调用函数,评估函数中的两个参数...)感谢你们两个;) – Defoe