0

I was manipulating an array with a for loop. How can I do this with a forEach loop and a function? I tried this, but I'm getting an undefined in the output.

var pets = ['cat', 'dog', 'rat'];

pets = pets.forEach( function plural(value, index, array){
    value = value + 's';
    return value;
})


/*for ( i = 0; i < pets.length; i++) {
    pets[i] = pets[i] + 's';
}
*/

console.log(pets);

2 Answers 2

1
pets.forEach( function plural(value, index){
    pets[index]  = value + 's';
})
Sign up to request clarification or add additional context in comments.

Comments

1
pets = pets.map( function plural(value, index, array){
    return value + 's';
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.