Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
John B

Chapter 5 Calculator

Recommended Posts

Hi everyone, I'm currently working on an assignment for my class that involves me creating a calculator and displaying certain form values with if then else statements. I attached a file to the assignment I'm working on below. So far my problem with this assignment is in ("Step 3, e") this step in the assignment requires me to create an if then else statement for hoursWorked between 0 and 80 and hourlyRate between 0 and 100. The next part of this assignment tells me if the conditions aren't met I must provide an error message, here is where I encountered problems in this assignment. When working on this assignment I got my error message to display but not in the right place, the error message is supposed to appear only during invalid data tests instead, the error message appears every time I click the submit button which I don't want it to do. I think the problem has to do with how I'm writing my if statement and I would appreciate it if anyone could look over my code and give me advice on how to display the error message properly, my code is listed down below. 

function calculate() {
    
    'use strict';
    
    var hoursWorked = document.getElementById('hoursWorked').value;
    
    if (hoursWorked.value <= 80 && hourlyRate.value <= 100) {
        
        } else { // Show an error:
    
     alert('Please enter valid hours and a valid payrate!');
    
    }
        
    var regularHours = document.getElementById('regularHours').value;
    
    var overtimeHours = document.getElementById('overtimeHours').value;
    
    var hourlyRate = document.getElementById('hourlyRate').value;
    
    var ficaTax = document.getElementById('ficaTax').value;
    
    var stateTax = document.getElementById('stateTax').value;
    
    var federalTax = document.getElementById('federalTax').value;
    
    var regularPay = document.getElementById ('regularPay').value = parseFloat(regularHours*hourlyRate).toFixed(2);
    
    var overtimePay = document.getElementById ('overtimePay').value = parseFloat(overtimeHours*(hourlyRate*1.5)).toFixed(2);
    
    var grossPay = document.getElementById ('grossPay').value = parseFloat(+regularPay+ +overtimePay).toFixed(2);
    
    var totalTaxes = document.getElementById ('totalTaxes').value = parseFloat(+ficaTax+ +stateTax+ +federalTax)* grossPay /100 .toFixed(2);
    
    var netPay = document.getElementById ('netPay').value = parseFloat(grossPay-totalTaxes).toFixed(2);
    
    
    
    var employeeName;
    
    var firstName = document.getElementById('firstName').value;
    
    var lastName = document.getElementById('lastName').value;
    
    employeeName = firstName + ' ' + lastName;
    
    document.getElementById('employeeName').value = employeeName;
    
    return false;
    
} // End of function.

function init() {
    
    'use strict';
    
    document.getElementById('form1').onsubmit = calculate;
    
} // End of init() function.

window.onload = init;

 

Paycheck2(1).pdf

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×