I have to add an item to my array through a function, but the code I made is not adding my item (orignalFlavors is my array).

function addFlavor(originalFlavors) { originalFlavors.unshift("Rainbow Sherbert"); } console.log(originalFlavors) 
1

2 Answers

You need to call the function addFlavor as well:

let originalFlavors = [...]; function addFlavor(originalFlavors) { originalFlavors.unshift("Rainbow Sherbert"); } addFlavor(originalFlavors); console.log(originalFlavors); 
1

Make sure you're defining the array, and then calling the function:

let originalFlavors = [] function addFlavor(of) { // use a different name to avoid shadowing and confusion of.unshift("Rainbow Sherbert"); return of; } originalFlavors = addFlavor(originalFlavors); console.log(originalFlavors); 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy