Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

368
Views
Really weird Javascript issue, how can I fix it?

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)

about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!