All puzzles
Per page
N
Parameters ยท Return

Leave a tip

Write the tip() function first. Two parameters are passed in: total and percent. It returns the tip, rounded to a whole number. Then, when someone clicks the tipButton button, find a 20 percent tip on the bill in the billInput text box. Show the tip in the tipLabel label.

If someone types 45 into the billInput text box, the label shows Tip: $9.

10 strips: N1 to N10

});
N1
function tip(total, percent) {
N2
onEvent("tipButton", "click", function() {
N3
var answer = tip(bill, 20);
N4
return Math.round(amount);
N5
setText("tipLabel", "Tip: $" + tip);
N6
var bill = getNumber("billInput");
N7
var amount = total * percent / 100;
N8
}
N9
setText("tipLabel", "Tip: $" + answer);
N10

Answer key: N. Leave a tip

Order: N2, N8, N5, N9, N3, N7, N4, N10, N1. Extra: N6

function tip(total, percent) {
	var amount = total * percent / 100;
	return Math.round(amount);
}
onEvent("tipButton", "click", function() {
	var bill = getNumber("billInput");
	var answer = tip(bill, 20);
	setText("tipLabel", "Tip: $" + answer);
});

Extra strip: setText("tipLabel", "Tip: $" + tip);. tip is the function itself, not what it returns. The tip it returned is in the answer variable.