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.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript maths problem

    I'm having trouble with my script. The script is suppose to calculate how many computers & netbooks it can fit on a bench. A computer can fit 4 per bench (ratio 4:1) and a netbook can fit 5 per bench (5:1). The problem is if I enter 3 computers & 2 netbooks it would give me 2 benches instead of 1. I think I did my maths wrong


    Code:
    <script type="text/javascript">
    do {
    computer = window.prompt("How many desktop computers do you require?", "0");
    computer_num = parseInt(computer);
    
    if (isNaN(computer_num)) /* If the user enters an invalid number it will prompt you to put a valid number */
    {
    window.alert("Enter a number 0 or above")
    }
    
    }
    
    while(isNaN(computer_num));
    
    computer_total = computer/4
    
    do {
    netbook = window.prompt("How many netbooks do you require?", "0");
    netbook_num = parseInt(netbook);
    
    if (isNaN(netbook_num))
    {
    window.alert("Enter a number 0 or above")
    }
    
    }
    
    while(isNaN(netbook_num));
    
    netbook_total = netbook/5
    
    benches = Math.ceil(computer_total) + Math.ceil(netbook_total);
    document.writeln ("<p>" + benches + " benches</p>");
    </script>

  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    No, you did your thoughts wrong. Just try it with real computers and benches.

  • #3
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Code:
    benches = Math.ceil(computer_total) + Math.ceil(netbook_total);
    You need the ceiling of the addition, not the sum of the ceilings:
    Code:
    benches = Math.ceil(computer_total + netbook_total);

  • #4
    New Coder
    Join Date
    Jan 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    benches = Math.ceil(computer_total + netbook_total);
    still gives me the answer of 2 benches if I input 3 computers & 2 netbooks

  • #5
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Quote Originally Posted by Ruriko View Post
    Code:
    benches = Math.ceil(computer_total + netbook_total);
    still gives me the answer of 2 benches if I input 3 computers & 2 netbooks
    3 computers = 0.75
    2 netbooks = 0.4


  •  

    Posting Permissions

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