Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/challenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@
// and returns the modified groceries list
function deleteThirdItem(groceries: string[]): string[] {
// write your code here...
return [];
if (groceries !== undefined && groceries.length > 3){
groceries.splice(2,1);
return groceries;
}else
return [];
}

// Step 2:
// Write a insertItemAtBeginning function that inserts a new grocery item at the beginning of the groceries argument
// and returns the modified groceries array
function insertItemAtBeginning(groceries: string[], item: string): string[] {
// write your code here...
if (groceries !== undefined){
groceries.unshift(item);
return groceries;
}else

return [];
}

Expand All @@ -21,6 +30,11 @@ function insertItemAtBeginning(groceries: string[], item: string): string[] {
// and return the modified groceries array
function replaceFirstTwoItems(groceries: string[]): string[] {
// write your code here...
if (groceries !== undefined && groceries.length > 2){
groceries[0] = 'ketchup';
groceries[1] = 'chili';
return groceries;
}
return [];
}

Expand Down
39 changes: 25 additions & 14 deletions src/groceries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,49 @@
// 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 arr1: string[] = ['apple','banana','orange','grape','strawberry','blueberry'];
return arr1; // 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...

return ""; // replace empty string with what you see is fit
if (groceries.length > 2 || groceries !== undefined)
return groceries[1]; // replace empty string with what you see is fit
else
return "";
}

// 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
if(groceries !== undefined)
return groceries.length; // replace zero with what you see is fit
else
return -1;
}

// Step 4:
// Write a getLastGroceryItem function that returns the last grocery item from `groceries` argument
function getLastGroceryItem(groceries: string[]): string {
// write your code here...

return ""; // replace empty string with what you see is fit
if(groceries !== undefined)
return groceries[groceries.length-1];
else
return ""; // 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 {
// write your code here...

return ""; // replace empty string with what you see is fit
if(groceries !== undefined){
let ItemL: string | undefined = groceries.pop();
return ItemL?ItemL:"";
}else
return ""; // replace empty string with what you see is fit
}

// Step 6:
Expand All @@ -47,16 +56,18 @@ function addNewGroceries(
itemTwo: string
): 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
if (groceries !== undefined){
return groceries.slice(0,3);
}else
return []; // replace empty array with what you see is fit
}

export {
Expand Down