Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript [Solved] Explain JS recursion

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #149330
    Kuzyo
    Participant

    Hi everyone. Can’t understand why my code doesn’t work. Console writes:

    Uncaught SyntaxError: Illegal break statement

       function writeNum( n ) {
      var i = n;
      if ( i === 0 ) break;
    
      console.log( i );
      i--;
      writeNum( n - 1 );
    }
    
    writeNum( 10 );
    

    Can somebody explain, please. Thanks

    #149335
    Anonymous
    Inactive

    @Kuzyo
    You don’t need break, because you haven’t for loop, or while loop, if you just want stops working code, when variable i = 10 (10, in this case), you just replace break to return false.
    full line: if ( i === 0 ) return false;

    #149388
    Kuzyo
    Participant

    @Developer9621 Thanks everything become clear :)

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.