28 lines
562 B
Markdown
28 lines
562 B
Markdown
# Parse an expression
|
|
|
|
An arithmetical expression consists of 2 numbers and an operator between them, for instance:
|
|
|
|
- `1 + 2`
|
|
- `1.2 * 3.4`
|
|
- `-3 / -6`
|
|
- `-2 - 2`
|
|
|
|
The operator is one of: `"+"`, `"-"`, `"*"` or `"/"`.
|
|
|
|
There may be extra spaces at the beginning, at the end or between the parts.
|
|
|
|
Create a function `parse(expr)` that takes an expression and returns an array of 3 items:
|
|
|
|
1. The first number.
|
|
2. The operator.
|
|
3. The second number.
|
|
|
|
For example:
|
|
|
|
```js
|
|
let [a, op, b] = parse("1.2 * 3.4");
|
|
|
|
alert(a); // 1.2
|
|
alert(op); // *
|
|
alert(b); // 3.4
|
|
```
|