SunQuest
 
           JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old July 13th, 2004, 04:58 PM
thetone thetone is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 1 thetone User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Red face Newbie really REALLY needs help ASAP

Hi all, I'm a newbie to javascript programming and am really stuck on something so silly.
I'm trying to create a function that will check a string of four characters and will only
return TRUE if all the characters are numeric.

I have made the function 'isNumeric' (shown below) and the function 'checkString' (also
shown below). Problem is, my 'checkString' function is wrong because everytime I run
it (as it is at the moment), I get TRUE if only the first character is numeric. The function
seems to ignore the other characters. Please can someone help.


function isNumeric(character)
/*
* Function returns true if its single-character string
* argument is a digit (0 to 9).
* Required for implementation of input checking function.
*/

{
return (character>='0')&&(character<='9')
}


function checkString(aString)

/*
* aString is a string. Function returns
* true if aString consists of a sequence
* of fours digits, false otherwise.
*/

{

if (aString.length != 4)
{
return false
};
for (var index = 0; index < 4; index = index + 1)
{
if (!isNumeric(aString.charAt(index)))
{
return false
}
else
{
return true
}
}
}

Thanks anyone who is nice enough to help me.

The Tone

Reply With Quote
  #2  
Old July 13th, 2004, 10:57 PM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Location: Sydney, AU
Posts: 2,058 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 6 m 11 sec
Reputation Power: 8
Send a message via ICQ to stumpy Send a message via MSN to stumpy
If you are checking for digits, then remove the quotes around the 0 and 9 in the isNumeric function - quotes signify an string, not a number.
__________________
DevArticles Moderator
BlueSix - Web Development and Consulting

Reply With Quote
  #3  
Old July 14th, 2004, 12:40 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
It's bailing after your first character because you're returning whether isNumeric returns true or false. So the function's always going to exit after the first loop iteration. Within the loop, return false only if isNumeric returns false. Then return true after the loop so you get a valid return if the condition inside the loop, but remove the else that returns true as the default behavior for each iteration.
__________________
Please don't PM me asking for solutions outside the scope of a thread.
Keeping all responses in a thread stands to help others who come along later,
which is after all what this forum's all about.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Newbie really REALLY needs help ASAP


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway