Basic Knowledge - Exercise 3
Question 1: Read the First Task Completed
Return the first item of a non-empty task array.
Expected output: "read" (print this with console.log)
function firstTaskItem(tasks) {
// Write your solution here
}
console.log(firstTaskItem(["read","write"]));function firstTaskItem(tasks) {
return tasks[0];
}
console.log(firstTaskItem(["read","write"]));Question 2: Read the Last Task Completed
Return the last item of a non-empty task array.
Expected output: "write" (print this with console.log)
function lastTaskItem(tasks) {
// Write your solution here
}
console.log(lastTaskItem(["read","write"]));function lastTaskItem(tasks) {
return tasks[tasks.length - 1];
}
console.log(lastTaskItem(["read","write"]));Question 3: Count Items in a Cart Completed
Each cart entry has a quantity. Return the total quantity across the entries.
Expected output: 5 (print this with console.log)
function countCartItems(items) {
// Write your solution here
}
console.log(countCartItems([{"quantity":2},{"quantity":3}]));function countCartItems(items) {
let total = 0;
for (const item of items) total += item.quantity;
return total;
}
console.log(countCartItems([{"quantity":2},{"quantity":3}]));Question 4: Append a Task Without Mutation Completed
Return a new array containing the original tasks followed by the supplied task. Do not modify the input array.
Expected output: ["read","write"] (print this with console.log)
function appendTaskCopy(tasks, task) {
// Write your solution here
}
console.log(appendTaskCopy(["read"], "write"));function appendTaskCopy(tasks, task) {
return [...tasks, task];
}
console.log(appendTaskCopy(["read"], "write"));Question 5: Place a Task First Completed
Return a new array containing the supplied task followed by the existing tasks.
Expected output: ["read","write"] (print this with console.log)
function prependTaskCopy(tasks, task) {
// Write your solution here
}
console.log(prependTaskCopy(["write"], "read"));function prependTaskCopy(tasks, task) {
return [task, ...tasks];
}
console.log(prependTaskCopy(["write"], "read"));Question 6: Remove the Last Task Completed
Return a new array without the final task. Do not modify the input.
Expected output: ["read","write"] (print this with console.log)
function withoutLastTask(tasks) {
// Write your solution here
}
console.log(withoutLastTask(["read","write","test"]));function withoutLastTask(tasks) {
return tasks.slice(0, -1);
}
console.log(withoutLastTask(["read","write","test"]));Question 7: Reverse a List Copy Completed
Return a new array containing the items in reverse order without modifying the input.
Expected output: [3,2,1] (print this with console.log)
function reverseListCopy(items) {
// Write your solution here
}
console.log(reverseListCopy([1,2,3]));function reverseListCopy(items) {
return [...items].reverse();
}
console.log(reverseListCopy([1,2,3]));Question 8: Check for a Cart Item Completed
Return true if the array includes the supplied item, otherwise false.
Expected output: true (print this with console.log)
function cartIncludesItem(items, item) {
// Write your solution here
}
console.log(cartIncludesItem(["pen","book"], "book"));function cartIncludesItem(items, item) {
return items.includes(item);
}
console.log(cartIncludesItem(["pen","book"], "book"));Question 9: Join a Shopping List Completed
Return the items joined by ", " as one string.
Expected output: "milk, bread" (print this with console.log)
function joinShoppingItems(items) {
// Write your solution here
}
console.log(joinShoppingItems(["milk","bread"]));function joinShoppingItems(items) {
return items.join(", ");
}
console.log(joinShoppingItems(["milk","bread"]));Question 10: Copy the First Two Items Completed
Return a new array containing at most the first two items.
Expected output: [4,7] (print this with console.log)
function firstTwoListItems(items) {
// Write your solution here
}
console.log(firstTwoListItems([4,7,9]));function firstTwoListItems(items) {
return items.slice(0, 2);
}
console.log(firstTwoListItems([4,7,9]));