Wrapping Up

The rest parameter is a good replacement for arguments—just as powerful minus the perils. The same symbol (...) when used on the calling side becomes the spread operator. In addition to these two additions related to function parameters/arguments, JavaScript now has the ability to assign default values to parameters. This last feature especially is very useful to extend existing functions for adding new parameters. In the next chapter, we will look at nice ways to iterate over collections of data.

Exercises

This chapter covered some significant ground and touched on many useful features in JavaScript. Before you move on to the next chapter, spend some time on these practice exercises as a way to review what you have learned in this chapter. You can find answers to these exerciseshere.

Exercise 1

An amountAfterTaxes() function returns the total amount after all the taxes are applied. Let’s implement that function so the output for each call in the next code shows up as expected.

 //the function goes here.
 
 const amount = 25.12;
 const fedTax = 10;
 const stateTax = 2;
 const localTax = 0.5;
 
 console.log(amountAfterTaxes(amount)); //25.12
 console.log(amountAfterTaxes(amount, fedTax)); //27.63
 console.log(amountAfterTaxes(amount, fedTax, stateTax)); //28.13
 console.log(
  amountAfterTaxes(amount, fedTax, stateTax, localTax)); //28.26

Exercise 2

The purchaseItems() function merely prints the parameters it receives, after a little formatting. Two calls to the function are shown. Let’s implement a third call to the function so that it produces the desired result.

 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

Exercise 3

Let’s reimplement the purchaseItems() function from the previous exercise so that milk and bread are assumed for the first two parameters, respectively, if a value is not provided for the argument in that position.

 const purchaseItems = //function declaration goes here
  console.log(essential1 + ', ' + essential2 + ', ' + optionals.join(', '));
 };
 
 const items = ['cheese', 'milk'];
 purchaseItems('cheese'); //cheese, bread,
 purchaseItems(...items); //cheese, milk,
 purchaseItems(); //milk, bread,

Exercise 4

The placeOrder() function assumes values for shipping and date if those values are not given. Let’s fix the parameter list so the function behaves as expected.

 const placeOrder = function(//...let's complete the parameter list...
  )
  console.log(' shipping charge for id: ' +
  id + ' is $' + shipping + ' Date:' + date.getDate());
 };
 
 //shipping, if not given, is $5 if amount less than 20 else $10
 //date is today's date unless given
 placeOrder(1, 12.10, 3, new Date('05/15/2018'));
 placeOrder(1, 25.20, 10);
 placeOrder(1, 12.05);
 placeOrder(1, 25.30);
 placeOrder(1, 25.20);

Exercise 5

In the previous example, how can we pass the value for the date parameter without passing a value for the shipping parameter?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.197.26