Reputation: 1
I have a program in js that is supposed to check for the carry number of a problem ie 55+20 = 0 carry number and total value = 75. There is something wrong with the check answer and I can't seem to figure out what it is. However when I use this method it doesn't give the correct output and says every answer is wrong.
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.");
}
}
Upvotes: 0
Views: 64
Reputation: 1446
my best guess is that you are messing with variable type
topNumber[1] = "35" telling the program that it's a piece of string, not int
I would try topNumber[1] = 35; and parseInt() the user input to be safe to compare
Upvotes: 1