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

178
Views
¿Por qué mi función sigue volviendo como indefinida?

Aquí está el código que tengo. Todavía soy bastante nuevo en javascript, así que no veo por qué agregar la función evita que la declaración for incremente los números enteros.

 const arr = [10,10,16,12] function incrementByOne(arr) { // arr is an array of integers(numbers), Increment all items in the array by // return the array for (const i = 0; i < arr.length; i++){ arr[i] += 1; } return arr }

Cualquier ayuda sería muy apreciada.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

const arr = [10,10,16,12] function incrementByOne(arr) { // arr is an array of integers(numbers), Increment all items in the array by // return the array for (let i = 0; i < arr.length; i++){ arr[i] += 1; } return arr } let a = incrementByOne(arr) console.log(a)
about 4 years ago · Juan Pablo Isaza Report

0

Ejecuté tu código en Node.js:

Código:

 'use strict'; const arr = [10,10,16,12] function incrementByOne(arr) { for (const i = 0; i < arr.length; i++) { arr[i] += 1; } return arr } console.log(incrementByOne(arr));

Producción:

 $ node incr.js incr.js:6 for (const i = 0; i < arr.length; i++){ TypeError: Assignment to constant variable. at incrementByOne (incr.js:6:37) $

Como puede ver, se queja de que está tratando de cambiar el valor de i que su código decía que era constante. Escriba let i = 0 para un valor no constante.

Código:

 'use strict'; const arr = [10,10,16,12] function incrementByOne(arr) { for (let i = 0; i < arr.length; i++) { arr[i] += 1; } return arr } console.log(incrementByOne(arr));

Producción:

 $ node incr.js [ 11, 11, 17, 13 ] $
about 4 years ago · Juan Pablo Isaza Report

0

Debe usar let i = 0 en lugar de const i = 0 para iniciar el contador de iteraciones de bucle i

 const arr = [10,10,16,12] function incrementByOne(arr) { // arr is an array of integers(numbers), Increment all items in the array by // return the array for (let i = 0; i < arr.length; i++){ arr[i] += 1; } return arr }

Luego llame a la función incrementByOne

 incrementByOne(arr);
about 4 years ago · Juan Pablo Isaza 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!