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

271
Views
How can I do addition with human words?

I'm trying to do addition with human words. My code takes the input, slices every word, and pushes into an array called DL. This works without any issues, and if you console.log it out, it does this:

[ 'add' ]
[ 'add', '6' ]
[ 'add', '6', 'and' ]
[ 'add', '6', 'and', '4' ]

Now I have an array in the code that are supposed to act as 'operators'. It looks like this:

var operator = [
  "add",
  "sub",
  "mul",
  "div"
]

Now I want to loop through the DL array to see if one of the operators is in DL. If this case passes true, I want to add 6 and 4 together, and the code will print out 10.

This is the full code:

function output(func) {
  console.log(func)
}

var operator = [
 "add",
 "sub",
 "mul",
 "div"
]

var dl = []

const read = string => {
   return string.split(' ').map(item => {
      dl.push(item.trim())
      console.log(dl)
   })
}


var main = read(
  `add 6 and 4`
)

output(main)
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

first, separate the operators and numbers from the array , you can either separate these two inside a .map or use a separate .filter

const operator = dl.find(x=>x.includes("add" , "sub" , "mul", "del"))
const numbers =  dl.filter(x=>Number(x))

then use a separate function to do the operations like

console.log(output())

function output(){
switch(operator){
case "add":
return numbers.reduce((a, b) => Number(a) +Number(b));
case "sub":
// your logic here
// rest of the operators logic

}

}
about 4 years ago · Santiago Gelvez Report
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!