From b5b3c11cc41812654be7fc0f18f1065694e02502 Mon Sep 17 00:00:00 2001 From: Wendy Date: Wed, 31 Aug 2022 12:35:49 -0500 Subject: [PATCH] met MVP --- 2index.js | 324 ++++++++++++++++++++++++++++++++++++++++++++++ index.js | 61 +++++---- package-lock.json | 4 +- 3 files changed, 365 insertions(+), 24 deletions(-) create mode 100644 2index.js diff --git a/2index.js b/2index.js new file mode 100644 index 00000000..ea0d9b3e --- /dev/null +++ b/2index.js @@ -0,0 +1,324 @@ +/* โ—โ— REMEMBER TO RETURN ALL OF THE ANSWERS ON THESE TASKS, IF YOU DON'T, THE AUTOGRADER WILL NOT WORK โ—โ—*/ + +/* ๐Ÿ‘€ This is your data โฌ‡ */ +const originalFlavors = [ + "Banana Nut Fudge", + "Black Walnut", + "Burgundy Cherry", + "Butterscotch Ribbon", + "Cherry Macaron", + "Chocolate", + "Chocolate Almond", + "Chocolate Chip", + "Chocolate Fudge", + "Chocolate Mint", + "Chocolate Ribbon", + "Coffee", + "Coffee Candy", + "Date Nut", + "Eggnog", + "French Vanilla", + "Green Mint Stick", + "Lemon Crisp", + "Lemon Custard", + "Lemon Sherbet", + "Maple Nut", + "Orange Sherbet", + "Peach", + "Peppermint Fudge Ribbon", + "Peppermint Stick", + "Pineapple Sherbet", + "Raspberry Sherbet", + "Rocky Road", + "Strawberry", + "Vanilla", + "Vanilla Burnt Almond" +] + +/*๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€ 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. +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 +*/ + + +function copy(array){ + return [...array]; +} +console.log('task 1:', copy(originalFlavors)); + + +/*๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€ 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 + 3. Your function should return a boolean TRUE if the length of the array is 31 and FALSE if the length of the array is NOT 31. + + +For Example: is31Flavors(originalFlavors) will return true if your code is working properly +*/ + +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. + +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 + 2. Receive a string in the second parameter that will take the new flavor as as an argument + 3. The function adds the passed flavor to the front of the passed array + 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"] +*/ + + +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. + +Use the removeLastFlavor function below to do the following: + 1. Receive an array + 2. Remove the last item from the received array + 3. Return the resulting array + + For example: running removeLastFlavor(originalFlavors) would return ["Rainbow Sherbert", "Banana Nut Fudge",..."Vanilla"] +*/ + + +function removeLastFlavor(array){ + array.pop(); + return array; +} +console.log('task 4:', removeLastFlavor(originalFlavors)); + + +/* ๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€ Task 5: ๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€ +Write a function that returns a flavor at a given index in the array. + +Use the getFlavorByIndex function below to do the following: + 1. Recieve an array in the first parameter that will take the flavors array as an argument + 2. Receive a number in the second parameter that will take the the desired index as an argument + 3. Return the flavor located at the received index position + + For example: running getFlavorByIndex(originalFlavors, 2) would return "Black Walnut", assuming Rainbow Sherbert has been added successfully +*/ + + +function getFlavorByIndex(array, number){ + return array[number]; +} + +console.log('task 5:', 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. + +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 + 2. Receive a string in the second parameter that will take the flavor name as as an argument + 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. + + HINT: You can use .splice() for this +*/ + +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. +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: + 1. Receive an array in the first parameter that will take the flavors array as an argument + 2. Receive a string in the second parameter that will take the filter value as as an argument (example: "chocolate") + 3. Check to see if any of the flavors in the array contain that string + 4. If they do, add them to a new array + 5. Return the new array that contains the filtered flavors + + For example: filterByWord(originalFlavors, "Chocolate") should return ["Chocolate", "Chocolate Almond", "Chocolate Chip", "Chocolate Fudge", "Chocolate Mint", "Chocolate Ribbon"] + + 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. +*/ + + +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('task 7:', filterByWord(originalFlavors, 'Chocolate')); + + +/* ๐Ÿ’ช๐Ÿ’ช๐Ÿ’ช๐Ÿ’ช๐Ÿ’ช๐Ÿง๐Ÿฆ๐Ÿจ 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. + +Use the getAverageWordLength function below to do the following: + 1. Receive the originalFlavors array + 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. +*/ + +function getAverageWordLength(/*code here*/){ + /*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 +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: + 1. Receive the four arrays with all the differnet flavors (originalFlavors is above, the others are below) + 2. Randomly pick flavors from all four arrays + 3. Return a new array called randomFlavors that has a lenght of 31 + + For example: getRandomFlavors(originalFlavors, newFlavors, seasonalFlavors, regionalFlavors) might return ["Strawberry Cheesecake", "Eggnog,"..."Chocolate"]. +*/ + + +function getRandomFlavors(/*code here*/){ + /*code here*/ +} + +// NEW DATA ARRAYS FOR STRETCH 2 โฌ‡๏ธ +// const newFlavors = [ +// "Date night", +// "U.S.S Butterscotch (Stranger Things special)", +// "Honey Almond", +// "Mint Chocolate Chip", +// "Chocolate", +// "Oreoยฎ Cookies'n Cream", +// "Chocolate Chip", +// "Pralines 'n Cream", +// "Very Berry Strawberry", +// "Chocolate Chip Cookie Dough", +// "Old Fashioned Butter Pecan", +// "Jamocaยฎ", +// "Jamocaยฎ Almond Fudge", +// "Reese'sยฎ Peanut Butter Cup", +// "Rocky Road", +// "Peanut Butter โ€™n Chocolate", +// "Gold Medal Ribbonยฎ", +// "World Classยฎ Chocolate", +// "Cherries Jubilee", +// "Chocolate Fudge", +// "Daiquiri Ice", +// "Rainbow Sherbet", +// "Rainbow Swirl" +// ] + +// const seasonalFlavors = [ +// "America's Birthday Cake", +// "Baseball Nutยฎ", +// "Blueberry Cheesecake", +// "Bourbon Street Pecan Pie", +// "Brownie Bar Mashup", +// "Cherry Cordial with Kisses", +// "Chocolate Mousse Royale", +// "French Vanilla", +// "Eggnog", +// "German Chocolate Cake", +// "Icing on the Cake", +// "Love Potion #31", +// "New York Cheesecake", +// "Nutty Coconut", +// "Peppermint", +// "Strawberry Cheesecake", +// "Rock โ€™n Pop Swirl", +// "Reeseโ€™s Peanut Butter Cup", +// "Trick Oreo Treat", +// "Winter White Chocolate", +// "made with Snickersยฎ", +// "made with M&M'sยฎ", +// "Heathยฎ", +// "Mango Tango" +// ] + +// const regionalFlavors = [ +// "Pink Bubblegum", +// "Caramel Macchiato", +// "York Peppermint Pattie", +// "Cotton Candy", +// "Orange Sherbet", +// "Grape Ice", +// "Watermelon Ice", +// "Miami Vice Sorbet", +// "Splish Splashยฎ", +// "Wild 'n Reckless Sherbet", +// "Lemon Custard", +// "Oregon Blackberry", +// "Bananas โ€˜n Strawberries", +// "Mississippi Mud", +// "Rum Raisin", +// "Creole Cream Cheese", +// "Chocolate Almond", +// "Fudge Brownie", +// "Banana Nut", +// "Black Walnut", +// "Cotton Candy Crackle", +// "Quarterback Crunch", +// "Chocolate Chocolate Chip Cheesecake", +// "Caramel 'n' Cookies" +// ] + + + +/* ๐Ÿ›‘๐Ÿ›‘๐Ÿ›‘๐Ÿ›‘๐Ÿ›‘๐Ÿ›‘๐Ÿ›‘๐Ÿ›‘๐Ÿ›‘๐Ÿ›‘ Please do not modify anything below this line ๐Ÿ›‘๐Ÿ›‘๐Ÿ›‘๐Ÿ›‘๐Ÿ›‘๐Ÿ›‘๐Ÿ›‘๐Ÿ›‘๐Ÿ›‘๐Ÿ›‘ */ +function foo(){ + console.log('its working'); + return 'bar'; +} +foo(); +module.exports = { + foo, + is31Flavors, + addFlavor, + removeLastFlavor, + getFlavorByIndex, + removeFlavorByName, + copy, + filterByWord, + getAverageWordLength, + getRandomFlavors +} + diff --git a/index.js b/index.js index e35f4076..ea0d9b3e 100644 --- a/index.js +++ b/index.js @@ -46,10 +46,10 @@ Use the copy function below to do the following: */ -function copy(/*your code here*/){ - /*your code here*/ +function copy(array){ + return [...array]; } - +console.log('task 1:', copy(originalFlavors)); /*๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€ Task 2: ๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€ @@ -62,11 +62,14 @@ 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: ๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€ @@ -82,10 +85,11 @@ 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('task 3:', addFlavor(originalFlavors, 'Rainbow Sherbert')); /* ๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€ Task 4: ๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€ @@ -100,10 +104,11 @@ 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('task 4:', removeLastFlavor(originalFlavors)); /* ๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€ Task 5: ๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€๐Ÿš€ @@ -118,11 +123,11 @@ 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('task 5:', 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. @@ -138,9 +143,15 @@ 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('task 6:', removeFlavorByName(originalFlavors, "Rocky Road")); @@ -163,10 +174,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('task 7:', filterByWord(originalFlavors, 'Chocolate')); /* ๐Ÿ’ช๐Ÿ’ช๐Ÿ’ช๐Ÿ’ช๐Ÿ’ช๐Ÿง๐Ÿฆ๐Ÿจ STRETCH ๐Ÿจ๐Ÿฆ๐Ÿซ๐Ÿ’ช๐Ÿ’ช๐Ÿ’ช๐Ÿ’ช๐Ÿ’ช*/ 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",