diff --git a/index.js b/index.js index e35f4076..ad6757c8 100644 --- a/index.js +++ b/index.js @@ -46,9 +46,11 @@ Use the copy function below to do the following: */ -function copy(/*your code here*/){ - /*your code here*/ +function copy(array){ + return [...array]; } +console.log(copy(originalFlavors)); + @@ -63,10 +65,15 @@ For Example: is31Flavors(originalFlavors) will return true if your code is worki */ -function is31Flavors(/*your code here*/){ - /*your code here*/ - } +function is31Flavors(array){ + if(array.length === 31){ + return true; + }else{ + return false; + } +} +console.log(is31Flavors(originalFlavors)); /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 3: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 @@ -82,11 +89,12 @@ Use the addFlavor function below to do the following: */ -function addFlavor(/*your code here*/){ - /*your code here*/ +function addFlavor(array, string){ + array.unshift(string) + return array; } - +console.log(addFlavor(originalFlavors, 'Rainbow Sherbert')); /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 4: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Houston, we have a problem! There are now 32 flavors in the originalFlavors array! Your task is to remove an item from the end of the array. @@ -100,11 +108,12 @@ Use the removeLastFlavor function below to do the following: */ -function removeLastFlavor(/*your code here*/){ - /*your code here*/ +function removeLastFlavor(array){ + array.pop(); + return array; } - +console.log(removeLastFlavor(originalFlavors)); /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 5: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Write a function that returns a flavor at a given index in the array. @@ -118,10 +127,12 @@ Use the getFlavorByIndex function below to do the following: */ -function getFlavorByIndex(/*your code here*/){ - /*your code here*/ +function getFlavorByIndex(array, number){ + return array [number]; } +console.log(getFlavorByIndex(originalFlavors)); + /*🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 6: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 @@ -138,10 +149,16 @@ Use the removeFlavorByName function below to do the following: HINT: You can use .splice() for this */ -function removeFlavorByName(/*your code here*/){ - /*your code here*/ +function removeFlavorByName(array, flavor){ + for(let i = 0; i < array.length; i++){ + if(array[i]=== flavor){ + array.splice(i,1); + } + } + return array; } +console.log(removeFlavorByName(originalFlavors,'Rochy Road')); /*🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 7: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 @@ -163,9 +180,16 @@ Use the filterByWord function below to do the following: */ -function filterByWord(/*your code here*/){ - /*your code here*/ +function filterByWord(array, flavor){ + let filteredArray = []; + for(let i = 0; i < array.length; i++){ + if (array[i].includes(flavor)){ +filteredArray.push(array[i]); + } + } + return filteredArray; } +console.log(filterByWord(originalFlavors, 'Chocolate')); diff --git a/package-lock.json b/package-lock.json index 48103f21..0f48f45e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "codegraded-project-js", - "version": "0.0.8", + "version": "0.0.9", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "codegraded-project-js", - "version": "0.0.8", + "version": "0.0.9", "devDependencies": { "@babel/core": "7.17.5", "@babel/plugin-transform-runtime": "7.17.0",