How to create a function that solves basic arithmetic operations

Here is one way to implement a function that takes a string as an input.

The string contains an arithmetic operation
1+2*3/4.

There are no parens or negative numbers.

calculatorTry in REPL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function solve(str) {
const toAdd = str.split('+');
const toSubtract = str.split('-');
const toMultiply = str.split('*');
const toDivide = str.split('/');
if (str.length === 1) {
return +str;
} else if (toAdd.length >= 2) {
return toAdd.reduce((accum, elem) => solve('' + accum) + solve(elem));
} else if (toSubtract.length >= 2) {
return toSubtract.reduce((accum, elem) => solve('' + accum) - solve(elem));
} else if (toMultiply.length >= 2) {
return toMultiply.reduce((accum, elem) => solve('' + accum) * solve(elem));
} else if (toDivide.length >= 2) {
return toDivide.reduce((accum, elem) => solve('' + accum) / solve(elem));
} else {
return +toAdd[0];
}
}

Tests can be found at REPL.it