Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

74
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda