943,172 Members | Top Members by Rank

Ad:
Aug 30th, 2010
0

For Loop Won't eEecute

Expand Post »
Hi im zee and i'm new to javascript..
The javascript function below..examines if a string value contains only numbers..
My problem is..I can't figure out why it doesn't continue with the 2nd loop and iterate j..please help.!

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="javascript">
  2. <!--
  3. value = "23e";
  4. isNumber(value);
  5.  
  6. function isNumber(value){
  7. document.write('I\'m inside the function isNumber' + '</br>');
  8. document.write('The value = ' + value + '</br>' );
  9.  
  10. document.write('The length of value = ' + value.length);
  11. var isAllValid=true; document.write(isAllValid);
  12. var validChars="0123456789"; document.write('</br>' + 'validChars = ' + validChars + '</br>');
  13. var current = "";
  14.  
  15. for(i=0; i<value.length; i++){
  16. current = value.charAt(i);
  17. document.write('current = ' + current + '</br>');
  18.  
  19. for(j=0; j<validChars.length; j++){
  20. document.write('j = ' + j + '</br>');
  21. document.write('current = ' + current + ' ' + 'validChars = ' + validChars.charAt(j) + '</br>');
  22. if(current == validChars.charAt(j)){
  23. document.write('break!');
  24. break;
  25. }
  26. if(j==validChar.length){
  27. document.write('oops! 1-9 evaluated...no match');
  28. isAllValid=false;
  29. break;
  30. }
  31. }
  32.  
  33. }
  34. return isAllValid;
  35. }
  36.  
  37. -->
  38. </script>
Last edited by poison_bubbles; Aug 30th, 2010 at 10:07 pm. Reason: wrong spelling
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
poison_bubbles is offline Offline
1 posts
since Aug 2010
Sep 1st, 2010
0
Re: For Loop Won't eEecute
Could you not just use the match method?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var str="A string";
  2. if(str.match(/^[0-9]+$/)){
  3. alert("Only Numbers");
  4. } else{
  5. alert("Not only numbers");
  6. }
  7. // Would alert "Not only numbers"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
avario is offline Offline
13 posts
since Apr 2009
Sep 1st, 2010
0
Re: For Loop Won't eEecute
Hi im zee and i'm new to javascript..
The javascript function below..examines if a string value contains only numbers..
My problem is..I can't figure out why it doesn't continue with the 2nd loop and iterate j..please help.!

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="javascript">
  2. <!--
  3. value = "23e";
  4. isNumber(value);
  5.  
  6. function isNumber(value){
  7. document.write('I\'m inside the function isNumber' + '</br>');
  8. document.write('The value = ' + value + '</br>' );
  9.  
  10. document.write('The length of value = ' + value.length);
  11. var isAllValid=true; document.write(isAllValid);
  12. var validChars="0123456789"; document.write('</br>' + 'validChars = ' + validChars + '</br>');
  13. var current = "";
  14.  
  15. for(i=0; i<value.length; i++){
  16. current = value.charAt(i);
  17. document.write('current = ' + current + '</br>');
  18.  
  19. for(j=0; j<validChars.length; j++){
  20. document.write('j = ' + j + '</br>');
  21. document.write('current = ' + current + ' ' + 'validChars = ' + validChars.charAt(j) + '</br>');
  22. if(current == validChars.charAt(j)){
  23. document.write('break!');
  24. break;
  25. }
  26. if(j==validChar.length){
  27. document.write('oops! 1-9 evaluated...no match');
  28. isAllValid=false;
  29. break;
  30. }
  31. }
  32.  
  33. }
  34. return isAllValid;
  35. }
  36.  
  37. -->
  38. </script>

There are 2 problems in this code.
1). line=26, "if(j==validChar.length)" you have variable name as validChars and int this line you used wrongly.

2). line=26, value of j and value of validChars.length will never match.


to optimize this code u can use parse() method.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
james chaco is offline Offline
24 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Javascript to php?
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Scrolling of images..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC