Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 5 of 5
Thread: Javascript maths problem

06032009, 12:40 PM #1
 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>
06032009, 12:56 PM
#2
 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.
06032009, 12:58 PM
#3
 Join Date
 Nov 2008
 Location
 not found
 Posts
 284
 Thanks
 0
 Thanked 53 Times in 51 Posts
You need the ceiling of the addition, not the sum of the ceilings:Code:benches = Math.ceil(computer_total) + Math.ceil(netbook_total);
Code:benches = Math.ceil(computer_total + netbook_total);
06032009, 01:01 PM
#4
 Join Date
 Jan 2009
 Posts
 10
 Thanks
 0
 Thanked 0 Times in 0 Posts
still gives me the answer of 2 benches if I input 3 computers & 2 netbooksCode:benches = Math.ceil(computer_total + netbook_total);
06032009, 02:20 PM
#5
 Join Date
 Nov 2008
 Location
 not found
 Posts
 284
 Thanks
 0
 Thanked 53 Times in 51 Posts