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.
"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");
alert(tmp);
tmp = decorate("Drake");
alert(tmp);
2. Extend the previous function to handle different data types, e.g., Numbers, Objects, ... . Show the data type into the returned string.
"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");
alert(tmp);
tmp = decorate(42); // a number
alert(tmp);
tmp = decorate({temperature: "25° C"}); // an object
alert(tmp);
3. Define a function that takes two parameters. The function returns the average of both.
"use strict";
// declare the function
function average(num1, num2) {
return (num1 + num2) / 2;
}
// call the function
alert(average(2, 4));
alert(average(2.4, 2.6));
4. Define the function of the first exercise with an arrow function.
"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");
alert(tmp);
// or, even more compact:
tmp = (str => "The given argument is: " + str + ". Ok?")("Drake");
alert(tmp);
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.
"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]);
alert(ret);
// 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".
"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;
}
alert(showCar("Ford", "Mustang", 300, "blue"));
alert(showCar("Ford", "Mustang GT", 350));