Javascript编码测验没有加起来

时间:2016-10-04 11:06:39

标签: javascript arrays sum

我正在尝试做一个简单的测验,它应该总结正确的答案和错误的答案。事情是,虽然我把三个正确的答案中的两个,我仍然得到正确和不正确的数组相同的结果:0。所以在评估函数的最后一定有一些错误。提前致谢

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
  	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++) 
	{
		if (responsesArray[i] === true|| "livescript" || "ruby") 
    {
      correct++;

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

2 个答案:

答案 0 :(得分:1)

定义一个数组来存储正确答案,然后比较正确和用户响应,并轻松识别它是否正确。

请查看下面的代码段。

&#13;
&#13;
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);
}
&#13;
&#13;
&#13;

答案 1 :(得分: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认为字符串是 truthy if条件始终为真。

另请注意,无需执行第二次if,因为else部分仅在第一个if条件为false时执行,这意味着您已经过滤了答案错误的情况。

最后,应在开始递增之前定义计数器变量。它在没有这个定义的情况下工作,但是如果两个变量中的一个没有递增,那么在调用evaluate之后它仍然是未定义的。最好总是定义你的变量。