JavaScript/Functions/Exercises

Topic: Functions

1. Define a function with one parameter. The function returns a string starting with "The given argument was: " and ends with ". Ok?". Insert the argument between those two strings.

2. Extend the previous function to handle different data types, e.g., Numbers, Objects, ... . Show the data type into the returned string.

3. Define a function that takes two parameters. The function returns the average of both.

4. Define the function of the first exercise with an arrow function.

5. Define a function that takes an array of numbers as its parameter. The function returns the array's length, the smallest element, the largest element, and the average of all elements.

Because a function can return only a single element, all those computed values must be packed into an array (or an object). This array is the return value.

6. Define a function for describing a car. It takes four parameters: producer, model, horsepower, and color. The first two are mandatory; the last two are optional. Define default values for the last two parameters, e.g., "unknown".

{What is the result?

+ Hello world - Runtime error - None of the above
 * type=""}

{What is the result?

- xxx, xxx, xxx - xxx, ppp, ppp + xxx, ppp, xxx - None of the above
 * type=""}