My latest project involves developing a calculator application. The buttons in the HTML code add elements to an array, and when the user presses "=", the app is designed to iterate through the array. It combines numbers that are directly next to each other without any mathematical symbols between them. However, I encountered a challenge where I have to remove an element every time two numbers are successfully combined.
let equation = [1,2,3,"/",1,2,3];
combineNumbers();
function combineNumbers()//call to start finding solution by merging numbers
{
for(let i = 1; i < equation.length;i++)//iterates length of equation starting at 1
{
if(isFinite(equation[i-1]) && isFinite(equation[i]))//checks if equation[i] and the index before it are numbers
{
equation[i-1] = '' + equation[i-1] + equation[i];//combines equation[i] and the index before
equation.splice[i];//removes element at index i
}
else
{
i++;
}
}
console.log(equation);
}
I experimented with iterating the length of the array backwards, but it only made things worse. Additionally, I tried various versions of the splice method, including
equation.splice[i]
equation.splice[i,1]
The current output using equation.splice[i] displays [12,23,3,"/",12,23,3], while the desired outcome should be [123,"/",123]