I am currently developing a calculator project that utilizes an array. My goal is to enable users to input multiple functions before obtaining the result, similar to the Casio fx-300ES Plus. At the moment, I am focusing on multiplication operations before progressing to other operators. I believe the most efficient approach is to identify the index of all 'x' characters using a for-loop, followed by two additional for-loops to examine the values on either side of the operator. Once another operator is encountered, the loop will terminate, allowing me to extract and store the relevant data adjacent to the 'x' symbol using slice().
However, I have encountered an issue when the numbers between operators are 1. Using slice() in such cases proves ineffective as there is no data between the specified indexes. Is there an alternative method to capture these numbers into an array?
Any insights or suggestions on this matter would be greatly valued.
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);
}
}