I have been working on a calculator using the eval() function, which returns a string (as shown in this tutorial). For instance, it may return something like 20+30
. Now, I am trying to split this string into an array of data, such as [20, 30, 50]
, where 20
and 30
are the operands, and 50
is the result.
So far, here's what I've attempted:
var input = document.getElementById('screen');
var result= '20+30'; // for example
var firstOperand = result.split('+', 1); // extracting the first operand
What I really need help with is converting my input value, which is a string like "20+30"
, into an array: myArr = [20, 30, 50]
.
Any assistance would be greatly appreciated!