Jump to content
Larry Ullman's Book Forums

John B

Members
  • Content count

    7
  • Joined

  • Last visited

Community Reputation

0 Neutral

About John B

  • Rank
    Newbie
  1. 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
  2. Hi HartleySan, I have some good news regarding my code. Recently I have been rewriting some of my equations for grossPay, netPay, totalTaxes etc and after testing the code in my paycheck calculator in Firefox it's safe to say I have my code and the paycheck calculator working, all I had to do was observe how you rewrote my code in your previous reply and I eventually did my best to rewrite my equations as I saw fit until I got the code working. Here is my newly updated code and the paycheck calculator is working as it should, thank you for all your all help and to responding to my messages. function calculate() { 'use strict'; 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;
  3. Thank you for trying I tested my paycheck.html file in Chrome and seems I got the error to appear in my browser console, here is a screenshot showing the error. I think what I will do is test my page in different browsers for errors and try to reorganize my code better, not only that but I will review how you rewrote my code and see if I can write my equations better using parseFloat in order to get the values to appear.
  4. Hi HartleySan I have another question, I think I am having an issue when it comes to the browser console I am able to pull it up through the Mozilla Firefox menu and then by going to the web developer menu but when I pull up the browser console it doesn't show me the list of errors in my code. I posted an attachment of what it looks like when I pull my browser console in Firefox.
  5. Hi HartleySan I recently made some changes to my code in Notepad++. So far I tried using the parseFloat function that way the values for regularPay, overtimePay and the other values I had would come out as decimals instead of strings but now, it seems like nothing is calculating when it did before and I can't get the values for grossPay, totalTaxes, and netPay to appear either. Here is my code below I have my equations set up as well the parseFloat function for each of my values but I still don't know why nothing is appearing when it should, do you think it has something to do with the way i'm writing the parseFloat function in my equations? function calculate() { 'use strict'; 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 = lastName + ', ' + firstName; 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;
  6. Hi HartleySan, thank you for responding to my question. I think I understand what your trying to tell me based on your reply, in my code both regularPay and overtimePay are presented as strings instead of numbers but my question is how do I convert regularPay and overtimePay into a number? Also, when regularPay and overtimePay are converted into numbers will the values for regularPay and overtimePay still appear in the calculator when I test the code?
  7. Hi Larry Ullman, I’m new to JavaScript and I’m currently working on an assignment for my class that involves me to create a paycheck calculator based on a form I created using Html elements in Notepad++ and elements of JavaScript in a separate Notepad++ document. The form I’m creating uses many aspects presented in chapter 4 of the Modern JavaScript Develop and Design textbook. The problem I’m having with my code is (posted below), so far when creating the paycheck calculator, I’ve got the regularPay and the overtimePay to calculate correctly but my main problem is whenever I try to get the calculations for the grossPay, netPay, and totalTaxes to calculate they won’t work at all. I would appreciate it if you looked over my code and gave me feedback on how to get the values to display. Here is my code for the Html form and below it is the JavaScript code. Here is the link to the assignment I’m working on as well. Assignment Link https://docs.google.com/file/d/0BwDHF5mNxWWZQm1aRkpCbUFkdkE/edit Html <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Paycheck Calculator</title> <link rel="stylesheet" href="css/styles.css"> </head> <body> <form action="#" method="post" id="form1"> <fieldset> <div><label for="firstName">First Name</label><input type="text" name="firstName" id="firstName" required></div> <div><label for="lastName">Last Name</label><input type="text" name="lastName" id="lastName" required></div> <div><label for="regularHours">Regular Hours Worked (between 0 and 40)</label><input type="text" name="regularHours" id="regularHours" value="0" required></div> <div><label for="overtimeHours">Overtime Hours Worked (between 0 and 40)</label><input type="text" name="overtimeHours" id="overtimeHours" value="0" required></div> <div><label for="hourlyRate">Hourly Rate (between 0 and 99.99)</label><input type="text" name="hourlyRate" id="hourlyRate" value="0.00" required></div> <div><label for="ficaTax">Fica Tax Rate (ex. 5.65)</label><input type="text" name="ficaTax" id="ficaTax" value="0.00" required></div> <div><label for="stateTax">State Tax Rate(ex. 28.00)</label><input type="text" name="stateTax" id="stateTax" value="0.00" required></div> <div><label for="federalTax">Federal Tax Rate (ex. 5.75)</label><input type="text" name="federalTax" id="federalTax" value="0.00" required></div> <div><label for="regularPay">Regular Pay</label><input type="text" name="regularPay" id="regularPay" value="0.00"></div> <div><label for="overtimePay">Overtime Pay</label><input type="text" name="overtimePay" id="overtimePay" value="0.00"></div> <div><label for="grossPay">Gross Pay</label><input type="text" name="grossPay" id="grossPay" value="0.00"></div> <div><label for="totalTaxes">Total Taxes</label><input type="text" name="totalTaxes" id="totalTaxes" value="0.00"></div> <div><label for="employeeName">Employee Name</label><input type="text" name="employeeName" id="employeeName"></div> <div><label for="netPay">Net Pay</label><input type="text" name="netPay" id="netPay" value="0.00"></div> <div><input type="submit" value="Calculate" id="submit"></div> </fieldset> </form> <script src="js/paycheck.js"></script> </body> </html> JavaScript function calculate() { 'use strict'; 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 = (regularHours*hourlyRate).toFixed(2); var overtimePay = document.getElementById('overtimePay').value = (overtimeHours*(hourlyRate*1.5)).toFixed(2); /*var grossPay = document.getElementById('grossPay').value = (regularPay+overtimePay).toFixed(2); var totalTaxes = document.getElementById('totalTaxes').value = (ficaTax+stateTax+federalTax*grossPay/100).toFixed(2); var netPay = document.getElementById('netPay').value = (grossPay-totalTaxes).toFixed(2); */ var employeeName; var firstName = document.getElementById('firstName').value; var lastName = document.getElementById('lastName').value; employeeName = lastName + ', ' + firstName; 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;
×