How to loop around and build an IP Address


var test = '1.1.%.%';
var tokens = test.split( '.' );
var tokenLimit = 11;

// start the recursion loop on the first token, starting with replacement value 0
makeIP( tokens, 0, 0 );

function makeIP ( tokens, index, nextValue ) {
  // if the index has not advanced past the last token, we need to
  // evaluate if it should change
  if ( index < tokens.length ) {
    // if the token is % we need to replace it
    if ( tokens[ index ] === '%' ) {
      // while the nextValue is less than our max, we want to keep making ips
      while ( nextValue < tokenLimit ) {
        // slice the tokens array to get a new array that will not change the original
        let newTokens = tokens.slice( 0 );
        // change the token to a real value
        newTokens[ index ] = nextValue++;
        
        // move on to the next token
        makeIP( newTokens, index + 1, 0 );
      }
    } else {
      // the token was not %, move on to the next token
      makeIP( tokens, index + 1, 0 );
    }
  } else {
    // the index has advanced past the last token, print out the ip
    console.log( tokens.join( '.' ) );
  }
}