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.

Click to see solution
```"use strict";

// declare the function
function decorate(str) {
const ret = "The given argument is: " + str + ". Ok?";
return ret;
}

// call the function
let tmp = decorate("Francis");

tmp = decorate("Drake");
```

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

Click to see solution
```"use strict";

// declare the function
function decorate(param) {

let dataType = "";
let paramAsString = "";

switch (typeof param) {
case "object":
dataType = "object";
paramAsString = JSON.stringify(param);
break;
case "string":
dataType = "string";
paramAsString = param;
break;
// next case ...
default:
dataType = "???";
paramAsString = param;
}
const ret = "The given argument is: " + paramAsString + " It's data type is: " + dataType + ". Ok?";
return ret;
}

// call the function
let tmp = decorate("Francis");

tmp = decorate(42);  // a number

tmp = decorate({temperature: "25° C"}); // an object
```

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

Click to see solution
```"use strict";

// declare the function
function average(num1, num2) {
return (num1 + num2) / 2;
}

// call the function

```

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

Click to see solution
```"use strict";

// declare the function and reverence it by a variable
const decorate = (str) => "The given argument is: " + str + ". Ok?";

// call the function
let tmp = decorate("Francis");

// or, even more compact:
tmp = (str => "The given argument is: " + str + ". Ok?")("Drake");
```

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.

Click to see solution
```"use strict";

function checkArray(arr) {

// local variables
let min = arr[0];
let max = arr[0];
let sum = 0;

// a 'forEach' construct with an anonymous function
arr.forEach((elem) => {
if (elem < min) { min = elem };
if (elem > max) { max = elem };
sum = sum + elem;
});

// the length and average can be computed here
// 'pack' everything in an array:  []
return [arr.length, min, max, sum / arr.length];
}

// the return value is an array
let ret = checkArray([2, 4, 6, 8]);

// or:
let [length, min, max, avg] = checkArray([2, 4, 6, 8, 10]);
alert ("Length: " + length + ", Min: " + min + ", Max: " + max + ", Average: " + avg);
```

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".

Click to see solution
```"use strict";

// supply default values if they are not given
function showCar(producer, model, horsepower = "unknown", color = "unknown") {

let ret = "";
if (!producer || !model) {
return "Producer and model must be specified";
}
ret  = "My car is produced by " + producer;
ret +=  ". The model is: " + model;
ret +=  ". The horsepower is: " + horsepower;
ret +=  ". The color is: " + color;

return ret;
}

```

1 What is the result?

```"use strict";

function func(param) {
return param + " world";
} //
```

 Hello world Runtime error None of the above

2 What is the result?

```"use strict";

function func(param) {
param = "ppp";
return param;
} //

let x = "xxx";