DEV Community

Discussion on: Higher-order functions in Javascript

Collapse
 
renatolazaroch profile image
Renato Lazaro • Edited

I believe you have set the variable n by mistake inside the for.

let array = [1, 2, 3, 4];
let newArray = [];
for (let i = 0; i < array.length; i++) {
newArray[i] = array[i] * 2;
}
console.log(newArray);
// 2, 4, 6, 8

=============================
High Order Functions

let numbers = [1, 2, 3, 4];
const result = numbers.map((n) => n * 2);
console.log(result);
// 2, 4, 6, 8