calculation - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# calculation

Posts: 3Member
I am attempting to write a calculation for javaScript.
The program prompts for the # of hours worked
also prompts for hourly pay.
< 40 is 1* hr rate
>40 and <50 is 1.5 hr rate
>50 <=60 2.0 hr rate
>60=2.5 hr rate

I can get the program to give me the hrs worked x pay rate but not figure out overtime. and then is should say
"you worked 50 hours at 10 dollars per hour therefore your weekly pay is 550 dollars."

• Posts: 6,349Member
: I am attempting to write a calculation for javaScript.
: The program prompts for the # of hours worked
: also prompts for hourly pay.
: < 40 is 1* hr rate
: >40 and <50 is 1.5 hr rate
: >50 <=60 2.0 hr rate
: >60=2.5 hr rate
:
: I can get the program to give me the hrs worked x pay rate but not figure out overtime. and then is should say
: "you worked 50 hours at 10 dollars per hour therefore your weekly pay is 550 dollars."
:
:
:
The simplest way is to conditionally sum the times together. First you calculate the amount of normal hours worked:
[code]
if (hours > 40) {
pay = 40*hr_rate;
} else {
pay = hours*hr_rate;
}
if ((hours > 40) and (hours < 50)) {
pay = pay + (hours-40)*hr_rate*1.5;
} else {
pay = pay + 10*hr_rate*1.5;
}
if ((hours > 50) and (hours < 60)) {
pay = pay + (hours-50)*hr_rate*2;
} else {
pay = pay + 10*hr_rate*2;
}
etc.
[/code]
Obviously there are much cleaner ways of calculating this (functions, loops), but this should work just fine.