diff --git a/src/challenge.ts b/src/challenge.ts index ad7177e..ab5f44d 100644 --- a/src/challenge.ts +++ b/src/challenge.ts @@ -5,7 +5,10 @@ // and returns the modified groceries list function deleteThirdItem(groceries: string[]): string[] { // write your code here... - return []; + // delete groceries[2]; this will produce an undefined value baaddd practice + groceries.splice(2, 1); // at index 2 delete one element, it will update the array length accordingly, look into mdn docs for splice + + return groceries; } // Step 2: @@ -13,7 +16,8 @@ function deleteThirdItem(groceries: string[]): string[] { // and returns the modified groceries array function insertItemAtBeginning(groceries: string[], item: string): string[] { // write your code here... - return []; + groceries.unshift(item); + return groceries; } // Step 3: @@ -21,7 +25,10 @@ function insertItemAtBeginning(groceries: string[], item: string): string[] { // and return the modified groceries array function replaceFirstTwoItems(groceries: string[]): string[] { // write your code here... - return []; + // this will replace not append new items + groceries[0] = "ketchup"; + groceries[1] = "chili"; + return groceries; } export { deleteThirdItem, insertItemAtBeginning, replaceFirstTwoItems }; diff --git a/src/groceries.ts b/src/groceries.ts index d8c867d..acd1b29 100644 --- a/src/groceries.ts +++ b/src/groceries.ts @@ -2,8 +2,15 @@ // Write a createGroceries function that returns an array with 6 groceries items (each item is a string) function createGroceries(): string[] { // write your code here... - - return []; // replace empty array with what you see is fit + let groceries: string[] = [ + "eggs", + "milk", + "flour", + "butter", + "vanilla extract", + "cocoa powder", + ]; + return groceries; // replace empty array with what you see is fit } // Step 2: @@ -11,15 +18,15 @@ function createGroceries(): string[] { function getSecondGroceryItem(groceries: string[]): string { // write your code here... - return ""; // replace empty string with what you see is fit + return groceries[1]; // replace empty string with what you see is fit } // Step 3: // Write a getGroceriesCount that returns the length of the `groceries` argument function getGroceriesCount(groceries: string[]): number { // write your code here... - - return 0; // replace zero with what you see is fit + let GroceriesCount: number = groceries.length; + return GroceriesCount; // replace zero with what you see is fit } // Step 4: @@ -27,15 +34,18 @@ function getGroceriesCount(groceries: string[]): number { function getLastGroceryItem(groceries: string[]): string { // write your code here... - return ""; // replace empty string with what you see is fit + let lastGroceryItem: string = groceries[groceries.length - 1]; + + return lastGroceryItem; // replace empty string with what you see is fit } // Step 5: // Write a removeLastGroceryItem function that removes the last grocery item and return it -function removeLastGroceryItem(groceries: string[]): string { +function removeLastGroceryItem(groceries: string[]): string | undefined { + // the OR undefined type added because the return function was throwing an error that pop returns either string or undefined // write your code here... - - return ""; // replace empty string with what you see is fit + let removedLastGroceryItem = groceries.pop(); + return removedLastGroceryItem; // replace empty string with what you see is fit } // Step 6: @@ -48,15 +58,22 @@ function addNewGroceries( ): string[] { // write your code here... - return []; // replace empty array with what you see is fit + groceries.push(itemOne); + groceries.push(itemTwo); + + return groceries; // replace empty array with what you see is fit } // Step 7: // Write a getFirstThreeGroceryItems function that returns a new array that contains the first three grocery items function getFirstThreeGroceryItems(groceries: string[]): string[] { // write your code here... - - return []; // replace empty array with what you see is fit + let firstThreeGroceryItems: string[] = [ + groceries[0], + groceries[1], + groceries[2], + ]; + return firstThreeGroceryItems; // replace empty array with what you see is fit } export {