1
JS Assignment 16: Using Callbacks in Array Methods
maximussallam (2)

function exerciseOne(names){
// Exercise One: In this exercise you will be given and array called names.
// Using the forEach method and a callback as it's only argument, console log
// each of the names.
}

function exerciseTwo(cents){
// Exercise Two: In this exercise you will be given an array called 'cents'
// This array is a list of prices, but everything is in cents instead of dollars.
// Using the map method, divide every value by 100 and save it as a new array 'dollars'

// Please write your answer in the lines above.
return dollars;
}

This is what I have:

function exerciseOne(names){
// Exercise One: In this exercise you will be given and array called names.
// Using the forEach method and a callback as it's only argument, console log
// each of the names.
console.log(names);
}

function eachMethod(array, cb){
for(let i = 0; i < array.length; i++){
cb(array[i]);
}
}
console.log(names.forEach(exerciseOne));

function exerciseTwo(cents){
// Exercise Two: In this exercise you will be given an array called 'cents'
// This array is a list of prices, but everything is in cents instead of dollars.
// Using the map method, divide every value by 100 and save it as a new array 'dollars'
return cents / 100;
}
dollars.map(exerciseTwo);
// Please write your answer in the lines above.
return dollars;
}

What did I do wrong?

You are viewing a single comment. View All
Answered by vedprad1 (461) [earned 5 cycles]
View Answer
2
malvoliothegood (90)

Study these examples: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
And:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Note that the map example can be written:

var array1 = [1, 4, 9, 16];

// This function doubles the input value and returns it
function doubler(x) {
    return x * 2;
}
// pass the function to map
const map1 = array1.map(doubler);

console.log(map1);
// expected output: Array [2, 8, 18, 32]