Help with my code

Hello, I need to pass an excercise with javascript.

I’m suppose to give the result for a bill dristribution of 5 with a 10% tip added to the amount.

this is my code:

const restaurantBill = (bill= 50) => {
const tax = bill*0.1;

const total= bill+tax;

return ["$" + total/5];
};

module.exports = restaurantBill;

I was given a boilerplate (template) with the code:

const restaurantBill = (bill) => {
const tax = /* ??? */

/* ??? */

return /* ??? */
};

If change it every possible I could and still it won’t acccept my answer. I don’t know what I’m doing wrong. Can someone help me please! I’m very new to javascript.

I think we need to know more about what is expected for the answer. Right now you are returning an array with one element and that element is a string representing a dollar amount. Is that what they want you to return?

  1. I have to asign the result from multipliying bill with 10% en the variable tax.
  2. I have to create a variable called total and asign it the result from adding bill plus tax
  3. I have to return the amount that each person have to pay (5 people) dividing total between 5, with the symbol $ at the beginning.

OK, so that would imply you need to return a string. But right now the string you are returning is in an array. Should you be returning an array or should you just be returning a string?

They suggested a string or string literals.

OK, so it sounds like they want a string returned from the function, not an array.

You are currently returning an array:

return ["$" + total/5];

Those brackets create an array and then you are putting the string you want to return as the first (and only) element in the array. So what do you think you need to do in order to return just the string?

I’ve tried:

  1. return “$” + “total /5”;
    2: return ("$ + total/5");
  2. return ("$" + “total/5”);
    none work. :thinking:

Each of those tries you listed doesn’t work because you are putting the expression total/5 inside a string (inside of double quotes). Putting that expression inside of a string makes it act like a string, so it will literally print “total/5” instead of doing the mathematically equation total/5.

Look at how you were returning the string when you were using an array.

return ["$" + total/5];

Notice how the expression total/5 is not inside of double quotes. You were doing it correctly there. The only issue was that you were returning an array instead of just a string. So you just need to do one thing to return the string itself instead of an array with a string in it.

One thing to keep in mind, when you use the + operator with both a string and a number, javascript will automatically convert the number to a string and then concatenate the two strings together. That is what was happening when you were returning the array. But it will also happen without the array.

I’ve tried now:

return “$”[total/5];
return “$(total / 5)”;
return ["$(total/5)"]
return ["$" + “(total/5)”]

I still don’t know what I’m missing. :worried:

Again, if you put total/5 inside of double quotes then it won’t work, so do not make any more attempts with total/5 inside of double quotes. That rules out the last three completely. I don’t want to see any more examples with total/5 inside of double quotes :slight_smile:

That means the only one that has even a chance of working is the first one:

return “$”[total/5];

Now you’ve got that array back in there which you don’t need. So another rule, no more arrays (i.e. no more square brackets)!

Remember, when you use the + operator to combine a string and a number then JS will automatically convert the number to a string and then concatenate them.

For example:

let stringNumber = "A" + 1;
console.log(stringNumber);

Notice that “A” is a string and 1 is a number. What do you think is going to be printed to the console? I’ll give you a hint, it is going to be a string. That’s because JS will automatically convert the number 1 to a string and then concatenate the “A” and the “1” together to make the string “A1”.

This should be enough information for you to get the return value on your function correct now.

1 Like

Thank you! I kept trying to enclose the math operation and it was so simple as to leave just the string and the operation do the math. (I even went as far as to add parseInt(). I’m very greatful for you help!

You’re welcome. We’ve all been there as we were learning too. I’ll bet you have a good grasp of this concept now and will never make this mistake again :slight_smile: Enjoy.

1 Like