Help with exercise

Asked

Viewed 1,248 times

5

Can anyone help me correct this exercise? I am trying to finish these lines and I cannot:


module.exports = function restaurantBill(bill) {

/*
  1. Crie uma variável chamada tax (imposto em inglês) e atribua-lhe o resultado
  de multiplique a conta em 10%.
*/

var tax = restaurantBill*0.10;

/*
  2. Crie uma variável chamada total e atribua-lhe o resultado da adição de conta
  mais impostos
*/


var total = restaurantBill+tax/5;

/*
  3. Retorne o valor que cada um deve pagar (total dividido por 5), com o
  símbolo $ antes (por exemplo: $ 11).
*/

return "$"+total;
};

Remarks:

Imagine you went out to eat with your four best friends. The total consumption account is 50 reais, but for this you must add 10% tax. You want to divide the account equally among the five.

For that you created this program.

Follow the steps below to complete the program and determine how much to pay each.

  • Create a variable called tax (tax in English) and assign it the result of multiplying the account by 10%. Tip: 10% in decimal is written 0.10.
  • Create a variable called total and assign it the result of adding account plus taxes
  • Return the amount that each must pay (total divided by 5), with the $ symbol before (for example: $ 11). Tip: you must use the string (concatenation of strings) to print with the $ symbol ahead.

Example:

var output = restaurantBill(50);
console.log(output); // --> $11
  • 1

    has part q should be the comment, but n is with the // to escape the rest of the commands

  • 1

    Does it give an error on the screen? An interesting detail is in restaurantBill+tax/5, which should be (restaurantBill+tax)/5 (otherwise it will do tax/5 and then add restaurantBill. Or even put the division in return "$"+total/5;

  • 1

    Make sure to mark the chosen question as accepted. See how in https://i.stack.Imgur.com/evLUR.png and see why in https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

  • Hello People! Thanks to you I was able to finish and identify my mistake! Thank you very much! Hug!

4 answers

4

You are using the function itself to do the calculation! To get the right return, you must pass in the formulas the parameter that the function receives, which in this case is bill.

Correcting your code would be:

module.exports = function restaurantBill(bill) {

    /*
      1. Crie uma variável chamada tax (imposto em inglês) e atribua-lhe o resultado
      de multiplique a conta em 10%.
    */

    var tax = bill*0.10;

    /*
      2. Crie uma variável chamada total e atribua-lhe o resultado da adição de conta
      mais impostos
    */


    var total = (bill+tax)/5;

    /*
      3. Retorne o valor que cada um deve pagar (total dividido por 5), com o
      símbolo $ antes (por exemplo: $ 11).
    */

    return "$"+total;
};
  • var total = (Bill+tax)/5;

  • True @Leocaracciolo! Had just copied the code, I will change here! (:

  • Another thing, how do you call the function?

  • you can call it as follows: restaurantBill(20) inside the script!

  • I believe it will make a mistake message": "Uncaught ReferenceError: module is not defined",

  • Yes, you’ll have to make one import { restaurantBill } from 'nome-arquivo'; correct?

  • https://answall.com/questions/55084/quando-usar-module-exports-ou-exports-no-node-js

  • Hello People! Thanks to you I was able to finish and identify my mistake! Thank you very much! Hug!

Show 3 more comments

2

Errors in your function: Using the function itself in the calculations.

Instead of var tax = restaurantBill*0.10;

can be var tax = restaurantBill.arguments[0]*0.10;

Instead of var total = restaurantBill+tax/5;

can be var total = (restaurantBill.arguments[0]+tax)/5;

I say "pode" because there is another alternative like the other answers published here. But I thought I should expand the range of options.

When a function receives parameter values of the instruction that invokes the function, these parameter values are silently assigned to the property arguments of the Function object. This property is an array of values, with the value of each parameter being assigned to a zero-based index entry in the array - even if there are no defined parameters. You can use array notation (nameFunction.Arguments[i]) to extract values from any parameters you want.

See the example below where the parameters are value and number of people

function restaurantBill(bill) {

   return "$"+((restaurantBill.arguments[0]+(restaurantBill.arguments[0]*.10)))/restaurantBill.arguments[1];

};
console.log (restaurantBill(50,5));

Following your working script:

    function restaurantBill(bill) {
        /*
          1. Crie uma variável chamada tax (imposto em inglês) e atribua-lhe o resultado
          de multiplique a conta em 10%.
        */
        var tax = restaurantBill.arguments[0]*0.10;
        
        /*
          2. Crie uma variável chamada total e atribua-lhe o resultado da adição de conta
          mais impostos
        */


        var total = (restaurantBill.arguments[0]+tax)/5;

        /*
          3. Retorne o valor que cada um deve pagar (total dividido por 5), com o
          símbolo $ antes (por exemplo: $ 11).
        */
        
         return "$"+total;

    };
    var output = restaurantBill(50);
    console.log(output); // --> $11

Another way:

    function restaurantBill(bill) {
        /*
          1. Crie uma variável chamada tax (imposto em inglês) e atribua-lhe o resultado
          de multiplique a conta em 10%.
        */
        var tax = bill*0.10;
        
        /*
          2. Crie uma variável chamada total e atribua-lhe o resultado da adição de conta
          mais impostos
        */


        var total = (bill+tax)/5;

        /*
          3. Retorne o valor que cada um deve pagar (total dividido por 5), com o
          símbolo $ antes (por exemplo: $ 11).
        */
        
         return "$"+total;

    };
    var output = restaurantBill(50);
    console.log(output); // --> $11

The formal syntax for a function is as follows:

 function nomeFunção ( [parâmetro] ....[parâmetro]) {
    instrução(ões)
 }

The parameters (also known as arguments) offer a mechanism to "deliver" a value from one instruction to another by means of a function call.

When a function receives parameters, it assigns the received values to the variable names specified in the parentheses of the function definition.

Consider the following script segment:

function restaurantBill(bill) {
 alerta(bill);
}
restaurantBill("Yra Rodrigues");

After the function is defined in the script, the next statement calls that same function, passing a string (Yra Rodrigues) as parameter. Setting the function automatically assigns the string to the variable bill. Therefore, before the Alert() statement within the function is executed, bill is assessed as Yra Rodrigues

Completion: use bill and not restaurantBill within the function!!!

wrong bill+tax/5; will add Bill to the tax division by 5

correct (bill+tax)/5; will divide by 5 the sum of Bill + tax

without much delay one can do so

function restaurantBill(bill) {

   return "$"+((bill+(bill*.10)))/5;

};
console.log (restaurantBill(50));

  • 2

    I believe that assigning the function to a variable can end up confusing even more someone who is starting Javascript studies.

  • Whatever, she did it in function call var output = restaurantBill(50);

  • Hello People! Thanks to you I was able to finish and identify my mistake! Thank you very much! Hug!

2


You made some mistakes in your code, but nothing to flog yourself on.

  1. You don’t need to use the module.export in your code for the moment, especially for such a simple exercise.

  2. Note that when doing the calculations, instead of calling the function parameter it would be (bill) you are calling the function itself.

  3. When doing accounts where there is a mutiplication or a division and you need to assign a fee to the total value before dividing it, do not forget to use parentheses, so that it makes the addition first before the division.

    var total = (bill + tax) / 5;

  4. Plus you were on the right path, only failed to assign a new variable to receive the function return result and then present it via console.log.

Follow the code below, hugs.

   function restaurantBill(bill) {

/*
    1. Crie uma variável chamada tax (imposto em inglês) e atribua-lhe o resultado
    de multiplique a conta em 10%.
*/

var tax = bill * 0.10;

/*
    2. Crie uma variável chamada total e atribua-lhe o resultado da adição de conta
    mais impostos
*/

var total = (bill + tax) / 5;

/*
    3. Retorne o valor que cada um deve pagar (total dividido por 5), com o
    símbolo $ antes (por exemplo: $ 11).
*/

return "$" + total;
};

var output = restaurantBill(50);
console.log(output);
  • Hello People! Thanks to you I was able to finish and identify my mistake! Thank you very much! Hug!

1

You were calling the function within itself, and at the time of division did not make the sum before, check now if it is right:

var module = function restaurantBill(bill) {
    /*
      1. Crie uma variável chamada tax (imposto em inglês) e atribua-lhe o resultado
      de multiplique a conta em 10%.
    */

    var tax = bill*0.10;

    /*
      2. Crie uma variável chamada total e atribua-lhe o resultado da adição de conta
      mais impostos
    */

    var total = bill+tax;

    /*
      3. Retorne o valor que cada um deve pagar (total dividido por 5), com o
      símbolo $ antes (por exemplo: $ 11).
    */

    var result = "$ "+(total/5);

    return result;
};
  • 2

    Yes! Changing the restaurantBill for Bill solves the problem. And the correction in the division, of course.

  • how to call the function?

  • calls the module() within the <script>

  • 1

    Hello People! Thanks to you I was able to finish and identify my mistake! Thank you very much! Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.