it’s really confusing me.
function arrayItemLengths() {
let sortedArray = ["anana", "ana", "ana", "nan"]
return sortedArray.forEach(
function(element){`${element}: length ${element.length}`})
}
console.log(arrayItemLengths())
thank you campers
it’s really confusing me.
function arrayItemLengths() {
let sortedArray = ["anana", "ana", "ana", "nan"]
return sortedArray.forEach(
function(element){`${element}: length ${element.length}`})
}
console.log(arrayItemLengths())
thank you campers
You’re not returning anything in the callback, so nothing happens.
return sortedArray.forEach(
function(element){return `${element}: length ${element.length}`})
apologies. this still doesn’t work though
What do you think this should do? I think you’re expecting it to do something it won’t do
hmmm
to take each element in the array, write out its name and length in a new array, and then return that array when the function is called.
thank you very much for the help
forEach
is designed for side effects - like adding things to the DOM, or logging to console. If you get rid of the return
inside the callback, and wrap the string in a console.log
, it’ll work how you expect it to but I think map
is the one that will actually do what you want it to do - in that, anything you put into it will spit out a list of all of those strings converted to the string you’ve defined.