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

72
Views
Convert Negative Numbers in Array
const numbers = [200, 450, -400, 3000, -650, -130, -70, 1300];

I want all negative numbers in this array to be Positive. How can I do that using Javascript?

//OUTPUT const numbers = [200, 450, 400, 3000, 650, 130, 70, 1300];
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

How about using map with Math.abs() to get the number's absolute value?

const notNegative = numbers.map((num) => Math.abs(num));

Since OP asked, you can use forEach, too, but you'd need to push results to a second array:

let notNegative = [];
numbers.forEach((num) => notNegative.push(Math.abs(num)));
about 4 years ago · Santiago Trujillo Report

0

You can write it even shorter:

const arr=[200, 450, -400, 3000, -650, -130, -70, 1300];

console.log(arr.map(Math.abs));

about 4 years ago · Santiago Trujillo Report

0

You can use absolute provided by Math as Math.abs() to convert all number into absolute number i.e. on positive side (whole number)

const numbers = [200, 450, -400, 3000, -650, -130, -70, 1300];
output=numbers.map(e=>Math.abs(e))
console.log(output)
// [200, 450, 400, 3000, 650, 130, 70, 1300]

about 4 years ago · Santiago Trujillo 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!