I am making a more "human" way to do math, and it's doing ok, however something is up with my code.
Basically, I'm telling the code to add 6 and 4 together and output it. It gives me what I'm expecting, somewhat. However the weird part about it, instead of printing straight just '10', it says:
[undefined, undefined, undefined, 10]
How can I just get it to return just '10'?
function output(func) {
console.log(func)
}
var dl = []
const read = str => {
return str.split(' ').map(item => {
dl.push(item.trim())
if (dl.length >= 4) {
const operator = dl.find(x => x.includes("add", "sub", "mul", "div"))
const numbers = dl.filter(x => Number(x))
switch (operator) {
case "add":
return numbers.reduce((a, b) => Number(a) + Number(b));
}
}
})
}
var main = read(
`add 6 and 4`
)
output(main)