-1

I am a very beginner in JavaScript and recently I am studying it on a book. I am trying to modify a script from this book that passes a number which is the number of letters in a word, and output the number of combinations of words you would get by scrambling the letters. While the original script lets you pass the number I want to modify it and allow the user to input a word, take it and return the number of words by scrambling the letters. My issue is that, when I run the code, I keep getting this error:

Uncaught TypeError: Cannot read property 'length' of undefined at thingamajig

Why is this happening?

var word = prompt('Word here.');

function thingamajig(word) {
    var facky = 1;
    clunkCounter = 0; //reset the clunkcounter
    if (word.length == 0) {
        display("clank");
    } else if (word.length == 1) {
        display("thunk");
    } else {
        while (word.length > 1) {
            facky = facky * word.length; //multiply facky with size until size is 0. Basically it is caulculating a factorial
            word.length = word.length - 1;
        }
        clunk(facky);
    }
}

function clunk(times) {
    var num = times;
    while (num > 0) {
        display("clunk"); //keep calling display f until it is = 0
        num = num - 1;
    }
}

function display(output) {
    console.log(output);
    clunkCounter = clunkCounter + 1; //How many times the clunck f called the display f?
}

thingamajig();
console.log(clunkCounter);
Andrea D_
  • 2,028
  • 3
  • 15
  • 38

1 Answers1

1

Since you are not passing the parameter in thingamajig() function, you can resolve that by changing your first if condition to check !word instead of the length of word like: if (!word)

Something which will look like:

if (!word) {
  display("clank");
} else if (word.length == 1) {
  display("thunk");
} else {
  while (word.length > 1) {
    facky = facky * word.length; //multiply facky with size until size is 0. Basically it is caulculating a factorial
    word.length = word.length - 1;
  }
  clunk(facky);
}
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62