diff --git a/index.js b/index.js index e35f4076..35487b33 100644 --- a/index.js +++ b/index.js @@ -36,23 +36,31 @@ const originalFlavors = [ ] /*🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 1: Copy the Array! 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 -We have an array called originalFlavors with 31 flavors (see above). In these tasks, we will be reading and writing data to this array. +We have an array called originalFlavors with 31 flavors (see above). In these tasks, we will be reading and writing data to this array. With all of these changes going on, we don't want to lose track of the actual, original 31 flavors. So we need to copy the original array! /* Use the copy function below to do the following: 1. receive an array as a parameter - you will pass in originalFlavors as an argument when the function is invoked. - 2. Return a copy of the received array + 2. Return a copy of the received array */ -function copy(/*your code here*/){ - /*your code here*/ +function copy(array){ + return [...array] } +console.log('task 1', copy(originalFlavors)); -/*🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 2: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 + + + + + + + +/*🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 2: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Confirm that an array is exactly 31 flavors. Your function should accept: 1. an array as a parameter 2. Check to see if the array given is 31 flavors @@ -62,15 +70,18 @@ Confirm that an array is exactly 31 flavors. Your function should accept: For Example: is31Flavors(originalFlavors) will return true if your code is working properly */ - -function is31Flavors(/*your code here*/){ - /*your code here*/ - } - +function is31Flavors(array){ + if(array.length === 31){ + return true; + }else{ + return false + } +} +console.log('task 2', is31Flavors(originalFlavors)) -/* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 3: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 -Corporate has come to you with an idea for a new flavor: Rainbow Sherbert! They think this will be a game changer. You need to modify the array to include this flavor. +/* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 3: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 +Corporate has come to you with an idea for a new flavor: Rainbow Sherbert! They think this will be a game changer. You need to modify the array to include this flavor. Use the addFlavor function below to do the following: 1. Receive an array in the first parameter that will take the flavors array as an argument @@ -79,17 +90,23 @@ Use the addFlavor function below to do the following: 4. The function should return the resulting array For example: addFlavor(originalFlavors, "Rainbow Sherbert") should return the array ["Rainbow Sherbert", "Banana Nut Fudge",..."Vanilla Burnt Almond"] + +.unshift('Rainbow Sherbert') + .unshift */ -function addFlavor(/*your code here*/){ - /*your code here*/ +function addFlavor(array, string){ + array.unshift(string) + return array; } + console.log('task 3', 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. +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. Use the removeLastFlavor function below to do the following: 1. Receive an array @@ -100,10 +117,13 @@ Use the removeLastFlavor function below to do the following: */ -function removeLastFlavor(/*your code here*/){ - /*your code here*/ +function removeLastFlavor(array){ + array.shift + return array; } +console.log('task 4', removeLastFlavor(originalFlavors)); + /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 5: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 @@ -118,14 +138,18 @@ 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, 2)) + /*🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 6: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 -As corporate wants to add more and more flavors to their lineup, they've realized that they need to remove flavors based on flavor name, as opposed to just arbitrarily removing the first or last flavor. Your task is to get an index by flavor name, and remove that single flavor from the array. +As corporate wants to add more and more flavors to their lineup, they've realized that they need to remove flavors based on flavor name, as opposed to just arbitrarily removing the first or last flavor. Your task is to get an index by flavor name, and remove that single flavor from the array. Use the removeFlavorByName function below to do the following: 1. Receive an array in the first parameter that will take the flavors array as an argument @@ -133,19 +157,28 @@ Use the removeFlavorByName function below to do the following: 3. Remove the received flavor from the received array 4. Return the resulting array that now contains one less flavor - For example: running removeFlavorByName(originalFlavors, "Rocky Road") would return an array with the a length of 30 because Rocky Road would have been removed. + For example: running removeFlavorByName(originalFlavors, "Rocky Road") would return an array with the a length of 30 because Rocky Road would have been removed. 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('task 6 ', removeFlavorByName(originalFlavors, 'Rocky Road')); + + /*🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 7: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 -July 7th is "World Chocolate Day" and Baskin Robins wants to create promotional materials highlighting all of their chocolate flavors. +July 7th is "World Chocolate Day" and Baskin Robins wants to create promotional materials highlighting all of their chocolate flavors. Your task is to write a function that checks every item in the array for a string and returns a new array called filteredArray with only the values that contain the received string. This would allow you to be able to filter for "Vanilla", "Sherbet", "Lemon" etc. when different holidays roll around by passing in those specific strings. Use the filterByWord function below to do the following: @@ -159,17 +192,26 @@ Use the filterByWord function below to do the following: HINT - you can use the .includes method to help you solve this - DO NOT USE ADVANCED ARRAY METHODS (i.e. .filter) to solve this problem. + DO NOT USE ADVANCED ARRAY METHODS (i.e. .filter) to solve this problem. */ -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, 'Choclate')) + -/* 💪💪💪💪💪🧁🍦🍨 STRETCH 🍨🍦🍫💪💪💪💪💪*/ +/* 💪💪💪💪💪🧁🍦🍨 STRETCH 🍨🍦🍫💪💪💪💪💪*/ /* STRETCH 1: Write a function that returns the average number of words in an array. You should be able to use this function for any array, but can test with originalFlavors. @@ -178,7 +220,7 @@ Use the getAverageWordLength function below to do the following: 2. Count how many words per item in the array 3. Return the average number of words per item in the array - For example: getAverageWordLength(originalFlavors) should return a number between 0 and 3. + For example: getAverageWordLength(originalFlavors) should return a number between 0 and 3. */ function getAverageWordLength(/*code here*/){ @@ -187,7 +229,7 @@ function getAverageWordLength(/*code here*/){ /* 💪💪💪💪💪💪💪💪💪💪 STRETCH 2: 💪💪💪💪💪💪💪💪💪 -Baskin Robins now offers new flavors, seasonal flavors, and even regional flavors. Write a function that will randomly select a total of 31 flavors +Baskin Robins now offers new flavors, seasonal flavors, and even regional flavors. Write a function that will randomly select a total of 31 flavors from originalFlavors, currentFlavors, seasonalFlavors, and regionalFlavors and store it in an array called randomFlavors. Use the getRandomFlavors function and new arrays below to do the following: @@ -228,7 +270,7 @@ function getRandomFlavors(/*code here*/){ // "Daiquiri Ice", // "Rainbow Sherbet", // "Rainbow Swirl" -// ] +// ] // const seasonalFlavors = [ // "America's Birthday Cake", @@ -304,4 +346,3 @@ module.exports = { getAverageWordLength, getRandomFlavors } - 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",