www.webdeveloper.com
Results 1 to 7 of 7

Thread: beginer quastion (return won't work)

  1. #1
    Join Date
    Jul 2012
    Posts
    4

    beginer quastion (return won't work)

    if i use return i; it only gives me the result of 1, but when i use console.log(i) it goes thru the whole list can would be helpful if someone please explain why it isn't working ?

    Code:
    var loop = function(){
      
      for( var i = 1; i < 100; i++ ){
      if(i % 5 ===  0 || i % 7 === 0){
      
      } else {
      console.log(i) ;
      }
      }         
    
    };
    
    loop();

  2. #2
    Join Date
    Nov 2007
    Posts
    409
    Your function does not include "return i" anywhere in it.
    I presume you mean for it to look like this:

    Code:
    <script type="text/javascript">
    var loop= function() {
    	for( var i = 1; i < 100; i++ ){
    		if(i &#37; 5 ===  0 || i % 7 === 0){
    			return i;
    		} else {
    			//document.write('False '+i+'<br>') ;
    		}
    	}         
    };
    
    document.write(loop());
    </script>

  3. #3
    Join Date
    Jan 2009
    Posts
    3,346
    "return" exits the function completely and doesn't continue executing the loop....console.log only appends your content to the console and doesn't exit the function at all...

  4. #4
    Join Date
    Jul 2012
    Posts
    4
    @nap0leon: I know, i meant it in place of console.log, however that is not the idea, the idea is to print the whole number list and skip those divisable by 5 and 7, the console.log(i) did it correct but i was just trying to understand return and why it didn't work.
    i'am testing it on codeacademy's console by the way.
    thanks


    @criterion9: Really ? i didn't find that info in any reference for return, anyway thanks for clearing THAT up, know where i can find more info about it ?

  5. #5
    Join Date
    Jul 2012
    Posts
    4

  6. #6
    Join Date
    Jul 2012
    Posts
    4
    @nap0leon: I know, i meant it in place of console.log, however that is not the idea, the idea is to print the whole number list and skip those divisable by 5 and 7, the console.log(i) did it correct but i was just trying to understand return and why it didn't work.
    i'am testing it on codeacademy's console by the way.
    thanks


    @criterion9: Really ? i didn't find that info in any reference for return, anyway thanks for clearing THAT up, know where i can find more info about it ?

  7. #7
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    If you want to return the entire list of numbers, you would have to pass back an array (or if you want to use a string, but that isn't as useful):

    Code:
    function loop()
    {
    	var list = [], i;
    	for (i = 1; i < 100; ++i) {
    		if (i &#37; 5 && i % 7) {
    			list.push(i);
    			console.log(i);
    		}
    	}
    	return list;
    }
    
    alert(
    	loop()
    );
    JavaScript: Learn | Validate | Compact

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles