Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What am I missing in my return function to creat outputs?

    I need to write a javascript program that takes in the details of three employees and creates a salary increase depending on the info they entered. I've created coding for the first employee, but no outputs are showing up. I think there is something wrong with my return function. Any advice or tips to improve my coding??

    [SIZE="2"][SIZE="1"]<script>


    function MainMethod()
    {
    var name1 = document.getElementById("name1").value;
    var salary1 = parseInt(document.getElementById("salary1").value);
    var department1 = document.getElementById("department1").value;
    var numofyears = parseInt(document.getElementById("numofyears1").value);

    var name2 = document.getElementById("name2").value;
    var salary2 = parseInt(document.getElementById("salary2").value);
    var department2 = document.getElementById("department2").value;
    var numofyears2 = parseInt(document.getElementById("numofyears2").value);

    var name3 = document.getElementById("name3").value;
    var salary3 = parseInt(document.getElementById("salary3").value);
    var department3 = document.getElementById("department3").value;
    var numofyears3 = parseInt(document.getElementById("numofyears3").value);

    //Employee 1
    if (salary1 <30000){
    if (numofyears1 >=2 && numofyears1 <4){
    var result1a = multiply(salary1,1.04);
    alert (name1 + result1a + department1);
    }
    else if (numofyears1 >=4 && numofyears1 <6){
    var result1b = multiply(salary1*1.05);
    alert (name1 + result1b + department1);
    }
    else if (numofyears1 >=6 && numofyears1 <8){
    var result1c = multiply(salary1*1.06);
    alert (name1 + result1c + department1);
    }
    else if (numofyears1 >=8){
    var result1d = multiply((salary1*1.07)+1000);
    alert (name1 + result1d + department1);
    }
    }
    else if (salary1 >=30000 && salary1 <40000){
    if (numofyears1 >=2 && numofyears1 <4){
    var result1e = multiply(salary1*1.02);
    alert (name1 + result1e + department1);
    }
    else if (numofyears1 >=4 && numofyears1 <6){
    var result1f = multiply(salary1*1.03);
    alert (name1 + result1f + department1);
    }
    else if (numofyears1 >=6 && numofyears1 <8){
    var result1g = multiply(salary1*1.04);
    alert (name1 + result1g + department1);
    }
    else if (numofyears1 >=8){
    var result1h = multiply((salary1*1.05)+1000);
    alert (name1 + result1h + department1);
    }
    }
    else if (salary1 >=40000){
    alert (name1 + salary1 + department1);
    }

    }

    function multiply(x,y){
    return x*y;
    }

    </script>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Nothing to do with java. Moving to javascript forum.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wait I'm confused...so with what I have coded, it should work?? Sorry, I'm new to this and my professor is no help at all.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,472
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Rather antiquated code. What is the target browser - Netscape 4?
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    Rather antiquated code. What is the target browser - Netscape 4?
    I'm using Eclipse to create the coding for Firefox. I'm using Firebug to find errors, but it isn't giving me anything...

  • #6
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Fou-Lu only said that he moved your thread because you had started it in the Java forum. However, this is Javascript and Java and Javascript are similar words, but very different languages.

    There are a few things "wrong" with your code, unfortunately right now I don't have much time. However, one mistake is this:

    Code:
    multiply(salary1*1.06);
    Either decide on salary * 1.06 or on multiply(salary, 1.06);, but not both, because this way the result of the expression will be salary * 1.06 * undefined, which is NaN.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,472
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Quote Originally Posted by BabyDais123 View Post
    I'm using Eclipse to create the coding for Firefox. I'm using Firebug to find errors, but it isn't giving me anything...
    So why are you using alert() instead of innerHTML for your output?
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    So why are you using alert() instead of innerHTML for your output?
    My professor instructed us to create the outputs as popups since we haven't learned about innerHTML. This is only my second time using JavaScript coding so I'm coding with what I'm familiar and with what he used in demonstrations. Is innerHTML a better option than alert()?

  • #9
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by felgall View Post
    Rather antiquated code. What is the target browser - Netscape 4?
    Using document.getElementById?

  • #10
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by BabyDais123 View Post
    My professor instructed us to create the outputs as popups since we haven't learned about innerHTML.
    Quite rightly so; A concept which an ever-growing band of anal retentives seem unable to grasp, especially in routines clearly destined never to go further than the author's own computer.

  • #11
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    Quite rightly so; A concept which an ever-growing band of anal retentives seem unable to grasp, especially in routines clearly destined never to go further than the author's own computer.
    Sooooo do you have any advice on how to fix this??? Or not...?

  • #12
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    You should show your code for the entire page. The way you put the Javascript inside the HTML and what the HTML looks like matters, too.

    By the way: I am glad to help, but please don't spam me with private messages. Anyone helping here will help when they find the time to.

  • #13
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Airblader View Post
    You should show your code for the entire page. The way you put the Javascript inside the HTML and what the HTML looks like matters, too.

    By the way: I am glad to help, but please don't spam me with private messages. Anyone helping here will help when they find the time to.
    Heres the entire thing...

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Pay Raises</title>
    </head>
    <script>


    function MainMethod()
    {
    var name1 = document.getElementById("name1").value;
    var salary1 = parseInt(document.getElementById("salary1").value);
    var department1 = document.getElementById("department1").value;
    var numofyears = parseInt(document.getElementById("numofyears1").value);

    var name2 = document.getElementById("name2").value;
    var salary2 = parseInt(document.getElementById("salary2").value);
    var department2 = document.getElementById("department2").value;
    var numofyears2 = parseInt(document.getElementById("numofyears2").value);

    var name3 = document.getElementById("name3").value;
    var salary3 = parseInt(document.getElementById("salary3").value);
    var department3 = document.getElementById("department3").value;
    var numofyears3 = parseInt(document.getElementById("numofyears3").value);

    //Employee 1
    if (salary1 <30000){
    if (numofyears1 >=2 && numofyears1 <4){
    var result1a = multiply(salary1,1.04);
    alert (name1 + result1a + department1);
    }
    else if (numofyears1 >=4 && numofyears1 <6){
    var result1b = multiply(salary1,1.05);
    alert (name1 + result1b + department1);
    }
    else if (numofyears1 >=6 && numofyears1 <8){
    var result1c = multiply(salary1,1.06);
    alert (name1 + result1c + department1);
    }
    else if (numofyears1 >=8){
    var result1d = multiply((salary1,1.07)+1000);
    alert (name1 + result1d + department1);
    }
    }
    else if (salary1 >=30000 && salary1 <40000){
    if (numofyears1 >=2 && numofyears1 <4){
    var result1e = multiply(salary1,1.02);
    alert (name1 + result1e + department1);
    }
    else if (numofyears1 >=4 && numofyears1 <6){
    var result1f = multiply(salary1,1.03);
    alert (name1 + result1f + department1);
    }
    else if (numofyears1 >=6 && numofyears1 <8){
    var result1g = multiply(salary1,1.04);
    alert (name1 + result1g + department1);
    }
    else if (numofyears1 >=8){
    var result1h = multiply((salary1,1.05)+1000);
    alert (name1 + result1h + department1);
    }
    }
    else if (salary1 >=40000){
    alert (name1 + salary1 + department1);
    }

    }

    function multiply(x,y){
    return x*y;
    }

    </script>
    <body>
    <h2>Pay Raises of Employees Program</h2>
    <form>

    <p>Employee 1</p>
    Name: <input id="name1" type="text" name="name1"/><br/>
    Base Salary: <input id="salary1" type="text" name="salary1"/><br/>
    Department: <input id="department1" type="text" name="department1"/><br/>
    Number of Years: <input id="numofyears1" type="text" name="numofyears1"/><br/> <br/> <br/>

    <p>Employee 2</p>
    Name: <input id="name2" type="text" name="name2"/><br/>
    Base Salary: <input id="salary2" type="text" name="salary2"/><br/>
    Department: <input id="department2" type="text" name="department2"/><br/>
    Number of Years: <input id="numofyears2" type="text" name="numofyears2"/><br/> <br/> <br/>

    <p>Employee 3</p>
    Name: <input id="name3" type="text" name="name3"/><br/>
    Base Salary: <input id="salary3" type="text" name="salary3"/><br/>
    Department: <input id="department3" type="text" name="department3"/><br/>
    Number of Years: <input id="numofyears3" type="text" name="numofyears3"/><br/> <br/> <br/>

    <input type="button" value="CALCULATE" onclick="MainMethod()"/>

    <br/><br/>
    <b>Output: </b>
    <p id="output">
    </p>


    </form>
    </body>
    </html>

  • #14
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    You declared "numofyears" instead of "numofyears1". Also "multiply((salary1,1.07)+1000);" is wrong (parantheses are not set correctly).

  • #15
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by BabyDais123 View Post
    Sooooo do you have any advice on how to fix this??? Or not...?
    You need a much more maintainable solution.
    I would tabulate the criteria, perhaps along these lines:

    Code:
    var riseRules = 
    [ 
       { maxSal, 30000,  maxYrs :  2,  incr : 1.02  }, 
       { maxSal, 30000,  maxYrs :  4,  incr : 1.03  }, 
       { maxSal, 30000,  maxYrs :  6,  incr : 1.04  }, 
       { maxSal, 30000,  maxYrs :  8,  incr : 1.05  },    
    
       { maxSal, 40000,  maxYrs :  2,  incr : 1.02  }, 
       { maxSal, 40000,  maxYrs :  4,  incr : 1.03  }, 
       { maxSal, 40000,  maxYrs :  6,  incr : 1.04  }, 
       { maxSal, 40000,  maxYrs :  8,  incr : 1.05  }    
    
    ];
    then loop through the elements until salary and service requirements are met, then read the corresponding increment.


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •