473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Non Negative Number Validation

I'm trying to have a user enter 4 numbers that are nonzero and non-negative.
This is what I have so far...

Expand|Select|Wrap|Line Numbers
  1. var intList = new Array(4); 
  2. var i=0; 
  3. for (i=0; i<4; i++) 
  4. intList[i]=prompt('Enter an integer',' '); 
  5.     if(!intList[i]<=0)
  6.         {
  7.         return false;
  8.         } else {
  9.         return true;
  10.         }
Not sure why it isn't working
Sep 27 '07 #1
5 6404
pbmods
5,821 Expert 4TB
Heya, Lonsalot. Welcome to TSDN!

Please use CODE tags when posting source code:

[CODE=javascript]
JavaScript code goes here.
[/CODE]

Couple of issues to tackle:
Expand|Select|Wrap|Line Numbers
  1.  if(!intList[i]<=0)
  2.         {
  3.         return false;
  4.         } else {
  5.         return true;
  6.         }
  7.  
Aside from the fact that this line does exactly the same thing:
Expand|Select|Wrap|Line Numbers
  1. return( intList[i] <= 0 );
  2.  
Your code is returning *false* if intList[i] > 0 (or ! <= 0, same thing).

And by *returning*, you break out of the loop.

A better method would be to continue to prompt the User until he enters a valid value (but allow him to cancel out of it).

Expand|Select|Wrap|Line Numbers
  1. for( var i = 0; i < 4; ++i )
  2. {
  3.     do
  4.     {
  5.         var int = prompt('Enter a number greater than zero:', 1);
  6.  
  7.         if( int === false )
  8.         {
  9.             return int;
  10.         }
  11.     }
  12.     while( int <= 0 );
  13.  
  14.     intList[i] = int;
  15. }
  16.  
Sep 28 '07 #2
When I load the page, nothing happens. I'm not sure what is wrong

Expand|Select|Wrap|Line Numbers
  1.  
  2. <html>
  3. <head>
  4. <title>Average Function</title>
  5.  
  6. <script language="javascript" type="text/javascript">
  7.  
  8. var intList = new Array(4); 
  9. var i=0; 
  10. for( var i = 0; i < 4; ++i )
  11. {
  12.     do
  13.     {
  14.         var int = prompt('Enter a number greater than zero:', 1);
  15.  
  16.         if( int === false )
  17.         {
  18.             return int;
  19.         }
  20.     }
  21.     while( int <= 0 );
  22.  
  23.     intList[i] = int;
  24. }
  25.  
  26. var a = parseInt(intList[0]);  //not sure if this is neccessary???
  27. var b = parseInt(intList[1]);
  28. var c = parseInt(intList[2]);
  29. var d = parseInt(intList[3]);
  30.  
  31. var avg = (a + b + c + d)/4;
  32.  
  33. alert(avg);
  34.  
  35. </script>
  36. </head>
  37. <body>
  38. </body>
  39. </html>
  40.  
  41.  
Sep 28 '07 #3
What does this statement mean?

Expand|Select|Wrap|Line Numbers
  1.  
  2. if( int === false )
  3.  
  4.  
Sep 28 '07 #4
dmjpro
2,476 2GB
What does this statement mean?

Expand|Select|Wrap|Line Numbers
  1. if( int === false )
  2.  
It is strict comparison.
As JavaScript is loosely typed language.
See the tutorial of JavaScript internal conversion :-)

Kind regards,
Dmjpro.
Sep 28 '07 #5
pbmods
5,821 Expert 4TB
Heya, Lonsalot.

To build on what DMJPro mentioned, if the User presses 'Cancel' at the prompt, then the value of int will be false.

The problem with, for example, doing this:
Expand|Select|Wrap|Line Numbers
  1. if( int == false )
  2.  
is that 0 would also resolve to false, which would break out of the loop. This would not be ideal behavior, so instead we use:
Expand|Select|Wrap|Line Numbers
  1. if( int === false )
  2.  
to ensure that we only break the loop if the User clicks on the Cancel button.


Random, but I thought I might as also well mention:
Expand|Select|Wrap|Line Numbers
  1. var intList = new Array(4);
  2.  
This creates a new Array and sets the value of the first element to 4. It does not create an Array with 4 elements. In JavaScript, you do not need to know how big your variables will be when you create them.


Now then.

If you're getting a blank page, the code is probably generating an error. In which browser are you testing this script?
Sep 28 '07 #6

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

Similar topics

16
by: JKop | last post by:
Take a class like the following: class Finger { public: double length; }
2
by: YM | last post by:
Hi, Need help making all values in the entire database negative regardless of what number is put in. If it's a VBA line, please indicate where I write it :) Thanks,
13
by: Ron | last post by:
Hi all I'm deciding whether to use the PK also as an account number, invoice number, transaction number, etc that the user will see for the respective files. I understand that sometimes a...
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
39
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,...
7
by: intrader | last post by:
The regular expression is /(?!((00000)|(11111)))/ in oRe. That is oRE=/(?!((00000)|(11111)))/ The test strings are 92708, 00000, 11111 in checkStr The expression used is checkStr.search(oRE). The...
6
by: =?Utf-8?B?ZGlhdG9tQG5ld3Nncm91cC5ub3NwYW0=?= | last post by:
Hello, I have a data entry windows form. One of the text boxes allows the user to enter a string. I need this text box to only allow users to type in a negative integer value (e.g. -1, -2,...
20
by: Casey | last post by:
Is there an easy way to use getopt and still allow negative numbers as args? I can easily write a workaround (pre-process the tail end of the arguments, stripping off any non-options including...
1
by: santoshsri | last post by:
Hi All, My C# web application calls a webservice to process a report. It sends XMLs as parameter and in response gets an XML node which stores Binay datatype bin.base64. It makes an instance of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.