Skip to content
← Back to Community
JS Assignment 16: Using Callbacks in Array Methods
Profile icon
kkleiman90

I have gotten to the last exercise of the last assignment in the precourse work for Web Development for Lambda and I am stuck. I am relatively new to the world of programming and after spending two days trying to figure this out on my own, I need some help!

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;
}

Answered by heyitsmarcus [earned 5 cycles]
View Answer
Voters
Profile icon
codeneenja1
Profile icon
heyitsmarcus
Profile icon
kkleiman90
Comments
hotnewtop
Profile icon
heyitsmarcus

I will help you get started and give you a hint, but I want you to figure this one out because it'll be hugely satisfying once you do! I promise!

But, here is a hint:

You're going to use the map method of course. What map does is applies some set of programming to every item in an array.

So, let's say I have an array of [10,20,30]. I want to divide each one of those by 10 and see the result:

console.log([10,20,30].map(item => item / 10));

You'll see that in the above I just divided everything in the array by 10 and the result was [1,2,3]. Use this same logic in your function!

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' const dollars = cents.map(item => //Here is where you need to divide everything by 100); //I used the parameter name "item" but you can name it to whatever valid variable name you would like // Please write your answer in the lines above. return dollars; }
Profile icon
ApoorvSingal

@heyitsmarcus That wasn't a hint. You told just everything.

Profile icon
heyitsmarcus

@ApoorvSingal This exercise is so simple that it's almost impossible to not give away almost everything. But, I didn't write in the final step.

Profile icon
kkleiman90

@heyitsmarcus thank you so much! The map method is difficult for me to wrap my head around for some reason.

Profile icon
heyitsmarcus

@kkleiman90 I hope it made more sense for you after this. I can write a little problem for you and help you through it with map if you'd like?

Profile icon
celinealmira

It looks really good, I'll do it now, love for you
driving directions

Profile icon
1Voice90

Same exercise . this is what I have so far

let DollarsInCents = cents.map(DollarsInCents / 100);