Basic Knowledge - Exercise 3

Question 1: Read the First Task

Return the first item of a non-empty task array.

Expected output: "read" (print this with console.log)

javascript

function firstTaskItem(tasks) {
  // Write your solution here
}

console.log(firstTaskItem(["read","write"]));

Question 2: Read the Last Task

Return the last item of a non-empty task array.

Expected output: "write" (print this with console.log)

javascript

function lastTaskItem(tasks) {
  // Write your solution here
}

console.log(lastTaskItem(["read","write"]));

Question 3: Count Items in a Cart

Each cart entry has a quantity. Return the total quantity across the entries.

Expected output: 5 (print this with console.log)

javascript

function countCartItems(items) {
  // Write your solution here
}

console.log(countCartItems([{"quantity":2},{"quantity":3}]));

Question 4: Append a Task Without Mutation

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)

javascript

function appendTaskCopy(tasks, task) {
  // Write your solution here
}

console.log(appendTaskCopy(["read"], "write"));

Question 5: Place a Task First

Return a new array containing the supplied task followed by the existing tasks.

Expected output: ["read","write"] (print this with console.log)

javascript

function prependTaskCopy(tasks, task) {
  // Write your solution here
}

console.log(prependTaskCopy(["write"], "read"));

Question 6: Remove the Last Task

Return a new array without the final task. Do not modify the input.

Expected output: ["read","write"] (print this with console.log)

javascript

function withoutLastTask(tasks) {
  // Write your solution here
}

console.log(withoutLastTask(["read","write","test"]));

Question 7: Reverse a List Copy

Return a new array containing the items in reverse order without modifying the input.

Expected output: [3,2,1] (print this with console.log)

javascript

function reverseListCopy(items) {
  // Write your solution here
}

console.log(reverseListCopy([1,2,3]));

Question 8: Check for a Cart Item

Return true if the array includes the supplied item, otherwise false.

Expected output: true (print this with console.log)

javascript

function cartIncludesItem(items, item) {
  // Write your solution here
}

console.log(cartIncludesItem(["pen","book"], "book"));

Question 9: Join a Shopping List

Return the items joined by ", " as one string.

Expected output: "milk, bread" (print this with console.log)

javascript

function joinShoppingItems(items) {
  // Write your solution here
}

console.log(joinShoppingItems(["milk","bread"]));

Question 10: Copy the First Two Items

Return a new array containing at most the first two items.

Expected output: [4,7] (print this with console.log)

javascript

function firstTwoListItems(items) {
  // Write your solution here
}

console.log(firstTwoListItems([4,7,9]));