0

I was about one hour trying to figure out why i am getting undefined from this function, the function works well, it stops when the condition triggers and the value of the randomAlphabet is correct, sure is a stupid bug but it makes me feel very annoying. Here is the code:

(function() {

  var getAlphabet = function() {
    var alphabet = [];
    for ( var asciiCode = 97; asciiCode < 123; asciiCode++ ) {
      alphabet.push(String.fromCharCode(asciiCode));
    }
    return alphabet;
  };

  var getRandomAlphabet = function(alphabet, randomAlphabet) {

    var alphabet = alphabet || getAlphabet();
    var alphabetLength = alphabet.length;
    var randomAlphabet = randomAlphabet || [];

    if ( alphabetLength === 0 ) {
      return randomAlphabet;
    };

    var getRandomIndex = function(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    };

    // remove the random item from the original alphabet
    var randomIndex = getRandomIndex(0, alphabetLength);
    var randomItem = alphabet.splice(randomIndex, 1)[0]; 

    // add the random item to the random alphabet
    randomAlphabet = randomAlphabet.concat(randomItem);

    getRandomAlphabet(alphabet, randomAlphabet);

  };

  // this returns undefined
  console.log(getRandomAlphabet()); 

})();
3
  • 2
    Because you're not returning anything from getRandomAlphabet? Commented Feb 23, 2015 at 11:48
  • return getRandomAlphabet(alphabet, randomAlphabet) Commented Feb 23, 2015 at 11:49
  • 1
    The function getRandomIndex should be near getAlphabet. It doesn't need to be redefined at each iteration. Commented Feb 23, 2015 at 11:52

1 Answer 1

2

Only thing is you need to return the getRandomAlphabet method

(function() {

  var getAlphabet = function() {
    var alphabet = [];
    for ( var asciiCode = 97; asciiCode < 123; asciiCode++ ) {
      alphabet.push(String.fromCharCode(asciiCode));
    }
    return alphabet;
  };

  var getRandomAlphabet = function(alphabet, randomAlphabet) {

    var alphabet = alphabet || getAlphabet();
    var alphabetLength = alphabet.length;
    var randomAlphabet = randomAlphabet || [];

    if ( alphabetLength === 0 ) {
      return randomAlphabet;
    };

    var getRandomIndex = function(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    };

    // remove the random item from the original alphabet
    var randomIndex = getRandomIndex(0, alphabetLength);
    var randomItem = alphabet.splice(randomIndex, 1)[0]; 

    // add the random item to the random alphabet
    randomAlphabet = randomAlphabet.concat(randomItem);

    return getRandomAlphabet(alphabet, randomAlphabet);

  };

  // this returns undefined
  console.log(getRandomAlphabet()); 

})();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.