By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,650 Members | 753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,650 IT Pros & Developers. It's quick & easy.

Input String, return Number to test null?

P: 1
I have a problem with my current code. I am trying to test the number for null and if it is, it will cancel my loop. It seems to cancel with the current way i have it, but i need the number to add up with the previous one each time it loops. How can i test for null to cancel, while getting my number to add up?

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.     <script type="text/javascript"> 
  4.  
  5.     function nameInput() {
  6.     var nameIn; 
  7.         nameIn = prompt("Enter the Employees name.");
  8.  
  9.         while(nameIn !== null &&(!isNaN(nameIn) || nameIn=="")) {
  10.  
  11.  
  12.         alert("Name cannot be Blank.\n must contain only LETTERS");
  13.         nameIn =prompt("Try again, Enter the employees name.");
  14. }    
  15.  
  16.     return nameIn;    
  17. }
  18.  
  19.         function hoursInput() {
  20.  
  21.  
  22.     var numIn=prompt("Enter the Hours worked for the Week");
  23.  
  24.         while(numIn !== null && (isNaN(numIn) || numIn=="")) {
  25.  
  26.  
  27.         alert("Hours cannot be Blank.\n must contain only NUMBERS.");
  28.  
  29.         numIn=prompt("Try again, Enter the Hours worked for the Week");
  30.  
  31.     }
  32.  
  33.     return numIn;    
  34. }
  35.  
  36. </script>
  37. </head>
  38. <body>
  39. <script type="text/javascript">
  40.  
  41. // DECLARATION
  42. var wage =10;
  43. var totalHours=0;
  44. var totalEarnings=0;
  45. var    Earnings;
  46.  
  47. // PROCESSING LOOP
  48. var howMany = parseFloat(prompt(" How many employees are you inputting?", ""));
  49.  
  50. for (var loopControl = 1; loopControl <= howMany; ++loopControl) {
  51.  
  52.     var nameth=nameInput();
  53.         if(nameth==null){
  54.             break;
  55. }
  56.  
  57.     var hour=hoursInput();
  58.  
  59.         if(hour==null){
  60.             break;
  61. }
  62.  
  63.             Earnings = hour * wage;
  64.             totalHours+=hour
  65.             totalEarnings+=Earnings;
  66.  
  67.             document.write("Name of employee. "   + nameth + "<br /br>");
  68.             document.write("This employee worked. "  + hour + "<br /br>" + "<br /br>");
  69.  
  70. }
  71.  
  72. // OUTPUT
  73.  
  74. document.write("compiled Earnings is: " + totalEarnings + " " + "<br /br>");
  75. document.write("total amount of hours from all employees listed are: " + totalHours + " " + "<br /br>");
  76.  
  77. </script>
  78. </body>
  79. </html>
Nov 12 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.