Bill Helper explainer

Aim of the puzzle: Use a helper function inside another function.
Walk through of solution: When writing a complicated function, it is easy for the code to get longer and longer and more difficult to read.

When this happens, it is helpful to break large functions up into smaller functions, and use these functions as “helpers” for a main function. This keeps the main function more readable.

In this puzzle, a function called calculateTax() is imported from additional code. This function can be used to help the puzzle’s splitBill() function.

To complete the puzzle, create a new variable inside the function and give it calculateTax(total) as a value. For example:

let tax = calculateTax(total);

Then, inside the function, add the variable to total using the += operator.

Finally, return the total divided by people.

Sample code solution:

import { calculateTax } from 'grasshopper.helperFunctions';

function splitBill(total, people) {
    let tax = calculateTax(total)
    total += tax;
    return total / people;
}

console.log(splitBill(76, 4));

Javascript Concepts: Functions, Helper Functions, console.log(), Variables, Local Variables, Return Statement, Imports, += Operator

This level will not give me the options to import {tax} from ‘grasshopper.helperFunctions’ or to import anything at all or to modify the import section that is already there or to create a variable that equals tax(total). Is this level bugged? I was not even allowed to pick the options for any solution I found.

I’ve updated the function in the explainer from tax() to calculateTax(). The import statement shouldn’t need to be edited in the app. If it still says, import { tax }, try resetting the puzzle to update it.

–Frankie

That makes so much more sense. I must have been close the first few times I tried and then when I went out to find an answer, I was really confused.

1 Like

What am I doing wrong?

We recently updated the puzzle to import calculateTax instead of tax. If you reset the puzzle, this should change.

Also, be careful not to use strings instead of variables or functions.

  • tax is a variable called tax
  • 'tax' is a just a string that says tax

Similarly with:

  • calculateTax(total) is a call to the calculateTax() function using the total variable as an argument.
  • 'calculateTax(total)' is a just a string of characters

–Frankie