1

I'm trying to match inputted text with a list of words from a pre-defined array. However, it's not returning anything, even the console.log doesn't return anything. I can't work out why the out loop isn't initiating. Any help would be greatly appreciated.

var actions = ["north", "south", "east", "west", "up", "down", "get", "take", "pick up", "use", "drop", "open", "close"];
var inputTextBox = document.getElementById("inputTextBox");

inputTextBox.addEventListener("keypress", function(event) {
    var stringArray = [];
    var x = event.which || event.keyCode; 
    var inString = inputTextBox.value.toLowerCase();

    if (x === 13) {
        stringArray = inString.split(" ");
        console.log("stringArray is --- " + stringArray + " --- length is " + stringArray.length);
        for (var i = 0; i < stringArray; i++) {
            console.log("outer loop is " + stringArray[i]);
            for (var j = 0; j < actions.length; j++) {
                if (stringArray[i] === actions[j]) {
                    console.log(stringArray[i]);
                }
            }
        }
    }
}
<input id="inputTextBox" type="text" maxlength="200" placeholder="words here" autofocus></input>
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
Cuckoo
  • 356
  • 1
  • 3
  • 14

2 Answers2

3

You forgot to use length property, because stringArray is an array and you have to iterate it, using its length.

for (var i = 0; i < stringArray.length; i++) {
 ................................^^^^

"However, it's not returning anything, even the console.log doesn't return anything."

To see the changes you have to press enter.

var x = event.which || event.keyCode;

event.which property indicates the specific key or button that was pressed and 13 is the key for enter command.

var actions = ["north", "south", "east", "west", "up", "down", "get", "take", "pick up", "use", "drop", "open", "close"];
var inputTextBox = document.getElementById("inputTextBox");

inputTextBox.addEventListener("keypress", function(event) {
 var stringArray = [];
 var x = event.which || event.keyCode; 
 var inString = inputTextBox.value.toLowerCase();
 

 if (x === 13) {
 stringArray = inString.split(" ");
 console.log("stringArray is --- " + stringArray + " --- length is " + stringArray.length);
  for (var i = 0; i < stringArray.length; i++) {
   console.log("outer loop is " + stringArray[i]);
   for (var j = 0; j < actions.length; j++) {
    if (stringArray[i] === actions[j]) {
     console.log(stringArray[i]);
    }
   }
  }
 }

});
<input id="inputTextBox" type="text" maxlength="200" placeholder="words here" autofocus></input>
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
1

In your first forloop, instead of i < stringArray try this i < stringArray.length

for (var i = 0; i < stringArray.length; i++) {
        console.log("outer loop is " + stringArray[i]);
        for (var j = 0; j < actions.length; j++) {
            if (stringArray[i] === actions[j]) {
                console.log(stringArray[i]);
            }
        }
    }
M3ghana
  • 1,231
  • 1
  • 9
  • 19