diff --git a/src/challenge.ts b/src/challenge.ts index ad7177e..1f1daa6 100644 --- a/src/challenge.ts +++ b/src/challenge.ts @@ -5,7 +5,8 @@ // and returns the modified groceries list function deleteThirdItem(groceries: string[]): string[] { // write your code here... - return []; + groceries.splice(2, 1); + return groceries; } // Step 2: @@ -13,7 +14,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 +23,8 @@ function insertItemAtBeginning(groceries: string[], item: string): string[] { // and return the modified groceries array function replaceFirstTwoItems(groceries: string[]): string[] { // write your code here... - return []; + groceries.splice(0, 2, "ketchup", "chili"); + return groceries; } export { deleteThirdItem, insertItemAtBeginning, replaceFirstTwoItems }; diff --git a/src/groceries.ts b/src/groceries.ts index d8c867d..5bc06b9 100644 --- a/src/groceries.ts +++ b/src/groceries.ts @@ -1,17 +1,24 @@ // Step 1: // Write a createGroceries function that returns an array with 6 groceries items (each item is a string) function createGroceries(): string[] { - // write your code here... + let groceries: string[] = [ + "milk", + "water", + "bread", + "cheese", + "rice", + "juice", + ]; - return []; // replace empty array with what you see is fit + return groceries; // replace empty array with what you see is fit } // Step 2: // Write a getSecondGroceryItem function that returns the second grocery item from `groceries` argument function getSecondGroceryItem(groceries: string[]): string { - // write your code here... + // 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: @@ -19,7 +26,7 @@ function getSecondGroceryItem(groceries: string[]): string { function getGroceriesCount(groceries: string[]): number { // write your code here... - return 0; // replace zero with what you see is fit + return groceries.length; // replace zero with what you see is fit } // Step 4: @@ -27,15 +34,15 @@ function getGroceriesCount(groceries: string[]): number { function getLastGroceryItem(groceries: string[]): string { // write your code here... - return ""; // replace empty string with what you see is fit + return groceries[5]; // 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 { // write your code here... - return ""; // replace empty string with what you see is fit + return groceries.pop(); // replace empty string with what you see is fit } // Step 6: @@ -48,15 +55,15 @@ function addNewGroceries( ): string[] { // write your code here... - return []; // replace empty array with what you see is fit + groceries.push(itemOne, 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 + return groceries.slice(0, 3); // replace empty array with what you see is fit } export {