[英]How do I loop a specified region of code to compile data in Javascript?
新手來了
所以我正在開發一個腳本來計算員工在他們的工作場所賺了多少錢。 我已經想出了如何提示用戶找出他們在某一天賺了多少錢,但是我如何循環這個代碼區域以便腳本繼續再次詢問問題以將總收入編譯成最終的每月值?
提前致謝。
const hourlyRate = 10.00 let hoursWorked = parseFloat(prompt("How many hours did you work?")) console.log("You worked a total of " + hoursWorked + " hours.") if (hoursWorked == 12) { hoursWorked -= 1 } else if (hoursWorked < 12 && hoursWorked >= 6) { hoursWorked -= 0.5 } console.log("You will be paid for " + hoursWorked + " hours of work due to unpaid breaks.") let incentiveRate = parseFloat(prompt("What was the hourly rate multiplier?")) let dayPay = hoursWorked * incentiveRate * hourlyRate console.log("You have earned £" + dayPay + " for your troubles today.")
將您的代碼包裝在 function 中,如下所示:
function loopFunction(){
//your code
return dayPay;
}
而不是添加這個
var totalDaysInMonth = 30;
var totalEarned = 0;
for(var i=0;i<totalDaysInMont;i++){
totalEarned += loopFunction();
}
此解決方案將詢問您 30 次問題,並且在循環之后您將獲得總收入。
show = true; while(show){ const hourlyRate = 10.00 let hoursWorked = parseFloat(prompt("How many hours did you work?")) console.log("You worked a total of " + hoursWorked + " hours.") if (hoursWorked == 12) { hoursWorked -= 1 } else if (hoursWorked < 12 && hoursWorked >= 6) { hoursWorked -= 0.5 } console.log("You will be paid for " + hoursWorked + " hours of work due to unpaid breaks.") let incentiveRate = parseFloat(prompt("What was the hourly rate multiplier?")) let dayPay = hoursWorked * incentiveRate * hourlyRate console.log("You have earned £" + dayPay + " for your troubles today.") let answer = parseFloat(prompt("Do you want do add the hours again? 0 for no, 1 for yes")) this.show = (answer === 0)? false: true; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.