Exercises
Recap Questions
Do all functions need to return something?
Not at all, for instance they can just console.log something.
How can we check what a function returns?
With the aid of console.log, like:
function sum(a, b) { return a + b; } console.log('The sum of 2 and 3 is', sum(2, 3));
Toggle Console Output
- The sum of 2 and 3 is 5
Exercises
Average of two numbers
Write a function called getAverage that takes two numbers as parameters, and returns their average value (i.e. the half of their sum).
Call the function three times with different arguments, and check if your expectation is met.
Important: the function does not need to log anything.
Possible solution
function getAverage(a, b) { return (a + b) / 2; } console.log(getAverage(2, 3)); console.log(getAverage(10, 20)); console.log(getAverage(5, -5));
Toggle Console Output
- 2.5
- 15
- 0
Max of two numbers
Write a function called max that takes two numbers as parameters, and returns the greater of the two.
Hint: you can use the return keyword inside an if block.
Call the function three times with different arguments, and check if your expectation is met.
Important: the function does not need to log anything.
Possible solution
function max(a, b) { if (a > b) { return a; } return b; } console.log(max(2, 3)); console.log(max(20, 10)); console.log(max(5, -5));
Toggle Console Output
- 3
- 20
- 5