2013-03-21 108 views
0

我有一个程序在js应该检查问题的进位数,即55 + 20 = 0进位数和总值= 75.检查答案有问题,我可以似乎没有弄清楚它是什么。但是,当我使用这种方法时,它没有给出正确的输出并且说每个答案都是错误的。Javascript问题与输出

var topNumber = new Array();//top number 
topNumber[1] = "35"; 

var bottomNumber = new Array();//bottom number 
bottomNumber[1] = "20"; 

var answer = Array();//answer 
answer[1] = "55"; 

var carryover = new Array();//carry over number 
carryover[1] = "0"; 

//create arrays to store user information 

var userCarry = []; 

var userAsn = []; 

//creates user values 

var userCarry1 = ""; 

var userAsn1 =""; 

//sets the number of questions to zero 
var qnumber = 0;  
//creates a function to display the page number, math problem, and an alert to save the user name. 
function nextQuestions() { 
    //every time the user clicks on the button the number will increase and will display question number and it will be used to display the rest of the question information. 
    qnumber = qnumber + 1; 
    document.getElementById('questionNumber').value = qnumber; 
     //sets up question numbers with qnumber is equal to a specific question number. 
    switch (qnumber) { 
     case 1: 
     // question number and question 
     document.getElementById('topQuestionNumber').value = topNumber[1]; 
     document.getElementById('bottomQuestionNumber').value = bottomNumber[1]; 
     //creates values to store the user information 
     userCarry1 = document.getElementById('carryOverNumber').value; 
     userAsn1 = document.getElementById('answerNumber').value; 
     break; 


//creates a function that will check if the user value is equal to the answer 

function checkAnswer() { 
    if((answer[1] == userAsn1) && (userCarry1 == carryover[1])) { 
     alert("Your answer is correct!" + userCarry1 + userAsn1); 
    } 
    if((answer[2] == userAsn2) && (userCarry2 == carryover[2])) { 
     alert("Your answer is correct! " + userCarry2 + " "+ userAsn2); 
    } 
    if((answer[3] == userAsn3) && (userCarry3 == carryover[3])) { 
     alert("Your answer is correct! " + userCarry3 + " "+ userAsn3); 
    } 
    if((answer[4] == userAsn4) && (userCarry4 == carryover[4])) { 
     alert("Your answer is correct! " + userCarry4 + " "+ userAsn4); 
    } 
    if((answer[5] == userAsn5) && (userCarry5 == carryover[5])) { 
     alert("Your answer is correct! " + userCarry5 + " "+ userAsn5); 
    } 
    if((answer[6] == userAsn6) && (userCarry6 == carryover[6])) { 
     alert("Your answer is correct! " + userCarry6 + " "+ userAsn6); 
    } 
    if((answer[7] == userAsn7) && (userCarry7 == carryover[7])) { 
     alert("Your answer is correct! " + userCarry7 + " "+ userAsn7); 
    } 
    if((answer[8] == userAsn8) && (userCarry8 == carryover[8])) { 
     alert("Your answer is correct! " + userCarry8 + " "+ userAsn8); 
    } 
    if((answer[9] == userAsn9) && (userCarry9 == carryover[9])) { 
     alert("Your answer is correct! " + userCarry9 + " "+ userAsn9); 
    } 
    if((answer[10] == userAsn10) && (userCarry10 == carryover[10])) { 
     alert("Your answer is correct! " + userCarry10 + " "+ userAsn10); 
    } 
    else { 
     alert("Your answer is wrong."); 
    } 


} 
+0

用调试器遍历代码。 – 2013-03-21 02:16:43

+0

switch语句的尾括号在哪里?第一个功能? – some 2013-03-21 03:34:58

回答

1

我最好的猜测是,你是用变量的类型搞乱

topNumber [1] =“35”,告诉它是一条绳子的程序,不是int

我会尝试topNumber [1] = 35;和parseInt()用户输入以便安全地进行比较

+0

你意识到'35 ==“35”'是真的,对吧? – RobG 2013-03-21 02:28:41

+0

我试着用parseint var userCarry = parseInt(document.getElementByld('carryOverNumber').value);并没有多大帮助。 – PandaMan 2013-03-21 03:07:04

+0

@RobG'35 ==“35”',但是'35 +“35”==“3535”' – some 2013-03-21 03:38:10