這應該做的伎倆:
(function(show, askFor){
var first = +askFor("The first number:"),
+second = askFor("The second number:");
show([
"You entered:", first, "and", second,
"Sum:", first+second
].join(" "));
})(alert, prompt);
(function(show, askFor){
var first = +askFor("The first number:"),
second = +askFor("The second number:");
show([
"You entered:", first, "and", second,
"Sum:", first + second,
"Difference:", first - second,
"Product:", first * second,
"Quotient:", first/second,
"Modulus:", first % second
].join(" "));
})(alert, prompt);
(function(show, askFor){
var first = +askFor("The first number:"),
second = +askFor("The second number:"),
third = +askFor("The third number:");
show("The average of those numbers: "+ (first+second+third)/3);
})(alert, prompt);
(function(show, askFor){
var hours = +askFor("How much you worked this week:"),
rate = +askFor("And what's your hourly rate:"),
overtimeRate = rate*1.5,
regularHours = Math.min(40, hours),
overtimeHours = Math.max(0, hours-40),
regularPay = rate*regularHours,
overtimePay = overtimeRate*overtimeHours;
show([
"Total Hours Worked:", hours,
" Regular Pay:", regularHours,
" hours @ $", rate, "/hour",
" = $", regularPay,
" Overtime Pay:", overtimeHours,
" hours @ $", overtimeRate, "/hour",
" = $", overtimePay,
" Total Pay: $", regularPay+overtimePay
].join(""));
})(alert, prompt);
請不要在此發佈您的作業。我不介意回答最基本的問題,因爲我自己也曾經是一名初學者。我很樂意幫助另一位編碼人員成長,但如果你不想工作,這完全是另一回事。 – Schien
這聽起來像家庭作業....至少包括一個小提琴向我們展示你已經做了什麼。 – Quinn
@Schien我正準備把OP放在同樣的事情上。我們首先喜歡代碼示例。 〜編輯:如同,向我們展示你到目前爲止所嘗試的內容,以及你的控制檯中可能出現的任何內容。 – Jason