Help with exercise

4

Can anyone help me correct this exercise? I'm trying to finish these lines and I can not:

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;
};

Comments:

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

For this you have created this program.

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

  • Create a variable called tax and assign it the result of multiplying the account by 10%. Tip: 10% in decimal is written 0.10.
  • Create a total call variable and assign it the result of account addition plus tax
  • 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
    
asked by anonymous 04.05.2018 / 18:48

4 answers

2

You have made some errors in your code, but nothing to whip yourself.

  • You do not have to use module.export in your code for now, even more so for a simple exercise.

  • See that in doing the calculations, instead of calling the parameter of the function that would be (bill) you are calling the function itself.

  • When making accounts where there is a multiplication or division and you need to assign a rate to the total value before dividing it, be sure to use parentheses so that it makes the addition first before dividing.

    var total = (bill + tax) / 5;

  • 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);
    
        
    04.05.2018 / 19:22
    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, it would look like:

    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;
    };
    
        
    04.05.2018 / 19:06
    3

    Errors in your function: Using the function itself in 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 posted here. But I thought I should expand the range of options.

      

    When a function receives parameter values from the statement that invokes the function, these parameter values are silently assigned to the arguments property 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 no parameters are defined. You can use array notation (function_name.arguments [i]) to extract the values of any parameters you want.

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

    function restaurantBill(bill) {
    
       return "$"+((restaurantBill.arguments[0]+(restaurantBill.arguments[0]*.10)))/restaurantBill.arguments[1];
    
    };
    console.log (restaurantBill(50,5));

    Following is 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)
     }
    

    Parameters (also known as arguments) provide a mechanism for "delivering" a value from one statement to another through a function call.

    When a function receives parameters, it assigns the received values to the variable names specified in the parenthesis 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 this same function, passing a string ( Yra Rodrigues ) as a parameter. The function definition automatically assigns the string to the variable bill . Therefore, before the alert () statement inside the function is executed, bill is evaluated as Yra Rodrigues

      

    Conclusion: use bill and not restaurantBill within function!

         

    wrong bill+tax/5; will add bill with tax division for 5

         

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

    Without much delay, you can do this

    function restaurantBill(bill) {
    
       return "$"+((bill+(bill*.10)))/5;
    
    };
    console.log (restaurantBill(50));
        
    04.05.2018 / 19:06
    1

    You were calling the function inside itself, and at the time of division did not add the sum before, now check if it works:

    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;
    };
    
        
    04.05.2018 / 19:08