A user inputs a math problem and the problem answers it. My code works for all 1 part problems such as "1+1" and "4*5". But after that it skips the last part of the problem. for example "1+1+1" outputs 2 and "1+1+1+1" outputs 3 and "4*5-6" outputs 20. What am I doing wrong here? I feel like it's simple but I tried a few things.
Relevant code:
function scan(i) {
"use strict";
var num,
schar = "",
strnum = "",
scanarray = [];
for (i; i <= input.length; i++ ) {
schar = input.charAt(i);
if (isoperator(schar)) {
break;
}
strnum = strnum + schar;
}
if (strnum !== "") { num = Number(strnum); }
scanarray[0] = schar;
scanarray[1] = i;
scanarray[2] = num;
return scanarray;
}
for (i; i <= input.length; i) {
scanarray = scan(i + 1);
schar = scanarray[0];
i = scanarray[1];
num = scanarray[2];
if (schar1 !== "") {
switch(schar1)
{
case "+":
answer = num1 + num;
break;
case "-":
answer = num1 - num;
break;
case "*":
answer = num1 * num;
break;
case "/":
case "÷":
answer = num1 / num;
break;
}
schar1 = "";
} else {
switch(schar)
{
case "+":
answer = answer + num;
break;
case "-":
answer = answer - num;
break;
case "*":
answer = answer * num;
break;
case "/":
case "÷":
answer = answer / num;
break;
}
}
}
I tried changing the for loop in the scan function to go until "i <= input.length + 1" and the for loop in the calculate function to the same and i tried changing both, but none of that worked. Any help is greatly appreciated!