Top Tipper explainer


Aim of the puzzle: Call a function inside another function’s definition.
Walk through of solution:
In the first two lines of the starter code, two variables have been declared: bill and tip. tip is initially set to 0, as its value will be calculated in the function calculateTip().

On the third line, the calculateTip() function is declared. It takes the argument total and updates the tip variable to be total * 0.2.

To complete the puzzle, call the function calculateTip() inside the body of addTip(). Give it total as an argument. Add it before the line return total + tip.

addTip() will use calculateTip() to calculate the appropriate value of the tip variable, and will then add this value to total. The result is returned with the return keyword.

When addTip() is called in the console.log() statement at the bottom of the code, it takes the bill variable as an argument. bill gets passed in as total to both addTip() and calculateTip(). The variable tip will be updated by calculateTip(), and then addTip() will return the sum of the two variables.

Sample code solution:

let bill = 40;
let tip = 0;
function calculateTip(total) {
    tip = total * 0.2;

function addTip(total) {
    return total + tip;

console.log('The total cost is ' + addTip(bill));

Javascript Concepts: Function Declarations, Function Calls, console.log, Variable Declarations with let