-1

I'm having so much trouble with javascript. I'm supposed to make a simple paycheck program using function. I have no idea what I'm doing wrong. I also have to change the payrate and taxrate to a decimal format. How do you put that in code? Here's what my code looks like.

<html>
  <body bgcolor="#81DAF5">
  <head>
    <title>Chapter 4 Assignment 1</title>
  </head>
  <body>
    <script type="text/javascript">
      var hoursworked = window.prompt("How many hours worked?", "");
      var payrate = window.prompt("What is the pay rate?", "");
      var taxrate = window.prompt("What is the tax rate?", "");
      var netpay
      parseFloat(payrate).toFixed(2);
      parseFloat(taxrate).toFixed(2);
      function calculatepay()
      {
        var grosspay= hoursworked * payrate;
        var taxamount= (grosspay * taxrate) / 100;
        var netpay= grosspay - taxamount;
        return netpay;
      }

      document.write("<h1><b>Hours Worked: " +hoursworked+ "<br></h1></b>");
      document.write("<h1><b>Hourly payrate: " +payrate+ "<br></h1></b>");
      document.write("<h1><b>Tax rate applied: " +taxrate+ "<br></h1></b>");
      document.write("<h1><b>Net Pay = " +calculatepay+ "<br></h1></b>");
    </script>
  </body>
</html>
Dave Chen
  • 10,887
  • 8
  • 39
  • 67
jaramore
  • 389
  • 1
  • 3
  • 12

4 Answers4

1

There are few problems in the code

  1. After parsing the input string, you need to assign the returned value back to the variable ex payrate = parseFloat(payrate).toFixed(2);
  2. You need to invoke the function calculatepay by adding () at the end to print the result

Try

  var hoursworked = window.prompt("How many hours worked?", "");
  var payrate = window.prompt("What is the pay rate?", "");
  var taxrate = window.prompt("What is the tax rate?", "");
  payrate = parseFloat(payrate).toFixed(2);
  taxrate = parseFloat(taxrate).toFixed(2);
  function calculatepay()
  {
    var grosspay= hoursworked * payrate;
    var taxamount= (grosspay * taxrate) / 100;
    var netpay= grosspay - taxamount;
    return netpay;
  }

  var netpay = calculatepay();

  document.write("<h1><b>Hours Worked: " +hoursworked+ "<br></h1></b>");
  document.write("<h1><b>Hourly payrate: " +payrate+ "<br></h1></b>");
  document.write("<h1><b>Tax rate applied: " +taxrate+ "<br></h1></b>");
  document.write("<h1><b>Net Pay = " +netpay+ "<br></h1></b>");

Demo: Plunker

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Thanks for the update. While parseFloat(payrate).toFixed(2) does indeed seem to convert the stored value into a float, it stores the answer as a string instead.

Also, you should probably assign the result to another variable or back into payrate in any case, or the float/string will be lost.

Try:

payrate = parseFloat(payrate).toFixed(2);
taxrate = parseFloat(taxrate).toFixed(2);

and then in your calculatepay() function:

// neat trick at http://stackoverflow.com/a/5661399/1091386
// allows you to use a string as a float in addition/subtraction
// apparently not necessary with multiplication but I thought I'd leave it in

var taxamount = hoursworked * +(payrate);

and finally, don't forget to call calculatepay() at the end with the () or you will just return the function.

icedwater
  • 4,701
  • 3
  • 35
  • 50
0

When u use parseFloat(payrate).toFixedString(2); The actual value is not changed.

EduardoSaverin
  • 545
  • 4
  • 19
0

I have updated your script a bit for you. A few small errors. You didnt include bracets when calling your calculate pay function, and you didnt store the result of applying the parseFloat method either.

applying the brackets gives:

document.write("<p>Net Pay = " + calculatepay() + "</p>"); 

storing the value:

payrate = parseFloat(payrate).toFixed(2);
taxrate = parseFloat(taxrate).toFixed(2);

also you dont need to declare each variable individually, you can do them all as a group by simply separating them with a comma:

var hoursworked = window.prompt("How many hours worked?", ""),
    payrate = window.prompt("What is the pay rate?", ""),
    payrate = parseFloat(payrate).toFixed(2),
    taxrate = window.prompt("What is the tax rate?", ""),
    taxrate = parseFloat(taxrate).toFixed(2),
    netpay = 0;

here is a working example - http://jsfiddle.net/2UVyH/2

hope that helps

lukeocom
  • 3,243
  • 3
  • 18
  • 30