Search⌘ K
AI Features

Solution Review: Purchase Items

Explore how to effectively use rest parameters and the spread operator to handle multiple function arguments in JavaScript. Learn to set default parameter values to prevent errors and produce predictable outputs, enhancing your coding flexibility and reliability.

We'll cover the following...

Task 1

Let’s dive into the solution to task 1.

Solution

Solution
Exercise
const purchaseItems = function(essential1, essential2, ...optionals) {
console.log(essential1 + ', ' + essential2 + ', ' + optionals.join(', '));
};
purchaseItems('bread', 'milk');
purchaseItems('bread', 'milk', 'jelly');
const mustHaves = ['bread', 'milk'];
const andAlso = ['eggs', 'donuts', 'tea'];
//call purchaseItems so it prints bread, milk, eggs, donuts, tea
purchaseItems(...mustHaves, ...andAlso);

Explanation

The first task requires us to add the third function call so that output is bread, milk, eggs, donuts, tea.

  • We have two arrays: mustHaves defined at line 8 and andAlso defined at line 9.

  • ...