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));
has part q should be the comment, but n is with the // to escape the rest of the commands
– alexandre9865
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 inreturn "$"+total/5;
– Cleber Griff
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
– user60252
Hello People! Thanks to you I was able to finish and identify my mistake! Thank you very much! Hug!
– Yra Rodrigues