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
Additional Code (hidden code that runs before the puzzle’s code):

let calculateTax = total => {
    return total * 0.15;
};
3 Likes

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.

1 Like

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

1 Like

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.

4 Likes

What am I doing wrong?

2 Likes

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

2 Likes

I am having same issues. Imported calculateTax has not updated to Tax. I think this is a purposeful bug intended to manipulate the user (specifically me) into creating an account, which I did specifically for the purpose of transmitting this communique, after which I am deleting the app from my phone… Which is too bad because I feel like I was learning something and for the most part I enjoy the teaching format/ method. Oh well, it’s not easy to live a life uncompromised. Good day, then, I just felt you should know there is a certain segment of people who feel a certain kind of way about enrollments and memberships and such but still quest for knowledge. I suppose if anything I said matters to you, you may respond to my email address as attached to this page. Thanks for what I got from it so far.

1 Like

I can’t compete the code
I tried everything but
Please help me

1 Like

Hi there,

Looks like your tax variable is storing the string 'calculateTax(total)' instead of the output of calculateTax(total).

To fix, delete 'calculateTax(total)'. In its place, select the calculateTax() key from the coding keyboard. Then, inside the parentheses (), add the total key from the coding keyboard.

Once you’ve made that change, your tax variable will be correctly storing the output of the calculateTax(total) function call.

Hope this helps!
Natalie

3 Likes

Thank You for the clarification :blush:
Loved learning basics of Code with Grasshopper

Thanks to all Grasshopper :heart: team

2 Likes

What am I doing wrong ?

1 Like

Hey there,

Try taking total += tax out of the parentheses of the calculateTax() function call. Move it to it’s own line, like this:

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

Thank you very much for your help I got it right now…

2 Likes

I don’t know what I am doing

1 Like

Hey there, try using the helper function that is being imported into the puzzle. Change the line

let tax = total

to

let tax = calculateTax(total) // this calculates the tax
total += tax // this adds the tax to the total

Hope this helps!

1 Like

@ let tax = calculateTax(total) you do not call the function you make a string out of it with the parentheses ‘stringHereNotAFunctionCall’ leave the ‘…’ and it will work

1 Like

you put the total+= tax in the brackets so its a argument put them under the function call

1 Like

here,this is how i passed the level

1 Like

What am I doing wrong?

1 Like

Hey there, try changing let tax = calculateTax to let tax = calculateTax(total). Use the calculateTax() key on the top row of the code keyboard.

Hope this helps!
Ben

1 Like