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