|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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.
|
|
#3
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Newbie really REALLY needs help ASAP |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|