Okay big question about switch statements. I'm new to JS.
I'm trying to make a switch statement that takes input from an input box, looks for "Multiplication" "Subtraction" "Addition" and "Division" among letters and numbers in the input string and separates non-numbers from numbers and then does the typed operation on the set of numbers. So for instance, the input box might look like this:
1 a 2 b 3 c 4 multiply d 5 e
So far, I've been able to separate numbers from non-numbers into arrays that would look like this given the input above:
numberArray === [1,2,3,4,5]
letterArray === [a,b,c,multiply,d,e]
and I have functions set to add, subtract, multiply, and divide the number array, so how would I incorporate using a switch statement to find one of those many possible inputs in my array of letters?
Another thing, all the loops used for the mathematical operations are similar, for instance, subtraction looks like this:
for (; i < numberArray.length; i++ ) {
if (i === 0) {
sub = numberArray[0]
} else {
sub = sub - numberArray[i]
}
}
and multiplication looks like this:
for (; i < numberArray.length; i++ ) {
if (i === 0) {
sub = numberArray[0];
} else {
sub = sub * numberArray[i];
}
}
Would it be possible to use the same switch statement to consolidate all four operation functions into one function, instead of calling each separate function for each case?
Edited to explain my letter and number arrays, also to change the title and tags from another topic that was entirely unrelated.