无法检测到二维数组

时间:2019-07-04 10:04:36

标签: javascript arrays

var exercise= [ 
     ['What is the first name of our professor in this course?', 'cecilia'],
     ['What is the last name of our prof in this coursrs?', 'chan'],
     ['Which language is the most popular one in the world?', 'javascript']
]; 

var player_ans;
var correct_num=0;
for(var i=0; i<exercise.length; i++){
     player_ans= prompt(exercise[i][0]);  
     if(player_ans.toLowerCase === exercise[i][1]){ 
        correct_num+= 1; 
     }
}
document.write('The number of question you answered correctly is '+ correct_num);

我希望正确回答所有问题后,我的correct_num的值为3。但是,屏幕上显示的correct_num的值仍为0 ...非常感谢您的帮助。谢谢。

3 个答案:

答案 0 :(得分:1)

toLowerCase是一个函数,因此将其用作toLowerCase()

var exercise= [ 
             ['What is the first name of our professor in this course?', 'cecilia'],
              ['What is the last name of our prof in this coursrs?', 'chan'],
              ['Which language is the most popular one in the world?', 'javascript']
]; 
var player_ans;
var correct_num=0;
    for(var i=0; i<exercise.length; i++){
       player_ans= prompt(exercise[i][0]); 
       
       if(player_ans.toLowerCase() === exercise[i][1])
         { correct_num+= 1;
         console.log(player_ans)
          }
     }
 document.write('The number of question you answered correctly is '+ correct_num);

答案 1 :(得分:1)

prompt()返回您传递给它的第二个变量。
并且您必须具有toLowerCase()进行比较。

player_ans= prompt(exercise[i][0],exercise[i][1]);       
if(player_ans.toLowerCase() === exercise[i][1].toLowerCase())
{ 
   correct_num+= 1;
   console.log(player_ans)
}

答案 2 :(得分:0)

toLowerCase是函数,这是一个问题,您应该具有此代码>

    var exercise= [ 
             ['What is the first name of our professor in this course?', 'cecilia'],
              ['What is the last name of our prof in this coursrs?', 'chan'],
              ['Which language is the most popular one in the world?', 'javascript']
]; 
var player_ans;
var correct_num=0;
    for(var i=0; i<exercise.length; i++){
       player_ans= prompt(exercise[i][0]);  

       if(player_ans.toLowerCase() === exercise[i][1])
         { correct_num+= 1; 
          }
     }
 document.write('The number of question you answered correctly is '+ correct_num);