I am working on a calculator project using an array. I wanted to allow the user to write multiple functions before finding the answer, similar to a Casio fx-300ES Plus. Right now I am working on multiplication before moving to other operators. To do this I thought the best way is to find the index at which all 'x' are located using a for-loop, then run two other for-loops, one looking at the left of the operator, the other looking at the right of it. Once it finds another operator, it will break. I could then store the information next to the 'x' using slice().
The problem I have run in to is when the numbers between operators is 1. If I use slice() it does not work as there is no information between the indexes. Is there another way to store these numbers into an array?
Any information on this is much appreciated.
var array = ['7', '3', '+', '6', 'x', '8', '+', '5', '4', 'x', '2'];
//for loop checking for 'x' symbols
for (var i = 0; i < array.length; i++){
console.log("i " + array[i]);
//if there is an 'x'
if (array[i] == 'x') {
console.log('index is at ' + i);
//create an array to eventually store the values
var newArray = new Array();
//checks for the index where j is NaN on the LEFT side
for (j = i - 1; j > 0; --j){
if (isNaN(array[j])){
console.log('j is ' + j);
break;
}
}
//checks for the index where e is NaN on the RIGHT side
for (e = i + 1; e < array.length; e++)
{
if (isNaN(array[e])){
console.log('e is at ' + e);
break;
} else if (e == array.length - 1) {
console.log('e is at array length of ' + e);
break;
}
}
//add the numbers between j and i to newArray
newArray = array.slice(j + 1, i);
console.log(newArray);
//add the numbers between i and e to newArray
newArray = array.slice(i + 1, e);
console.log(newArray);
console.log("array of slice is " + newArray);
}
}