Home > Back-end >  Filtering List By First Letter
Filtering List By First Letter

Time:12-15

I'm having a bit of trouble filtering my list. I want to filter it to where it checks the first letter of a word the user inputs and if that first letter is the same as the random letter from the "letters" list. If true, it adds that word the user inputted to the list and if not, it doesn't add it. I hope this makes sense, please help.

// list of words user inputs var guessedList = []; // list of random letters var letters = getColumn("American Sign Language Alphabet", "Letter");

function randomLetter() {
  var index = 0;
  index = randomNumber(0, letters.length);
  setText("letterOutput", letters [index]);
}
function checkWords(firstUserLetter) {
  var wordsMade = getText("wordInput");
  for (var i = 0; i < letters.length; i  ) {
     var firstLetter = letters [i].substring(0,1);
     firstUserLetter = wordsMade.substring(0,1);
     if (firstLetter==firstUserLetter) {
      // uses appendItem() to add a new item to the word list
      appendItem(guessedList, getText("wordInput"));
      setProperty("wordInput", "text", "");
      setText("wordsOutput", guessedList.join("\n"));
      updateScore();
      }
  }
}

I tried everything but I can't seem to figure out why it's not filtering it to where it only adds the word to the list if the first letter of it corresponds with the random letter.

CodePudding user response:

In the checkWords function, you are looping through the letters list and comparing the first letter of each letter with the first letter of the user's input. However, this is not what you want to do because you only want to compare the first letter of the user's input with the random letter that was generated, not with all the letters in the letters list right?

In the checkWords function, you are using the setText method to set the value of the wordsOutput element to the guessedList array. This will not work because the setText method expects a string as its argument, but you are passing it an array. To fix this, you can use the join method to convert the guessedList array into a string with each word separated by a newline character, and then pass this string to the setText method.

something like below might work:

var guessedList = [];    
var letters = getColumn("American Sign Language Alphabet", "Letter");

function randomLetter() {
var index = 0;
index = randomNumber(0, letters.length);
setText("letterOutput", letters [index]);
}

function checkWords(firstUserLetter) {    
var randomLetter = letters[randomNumber(0, letters. Length)];

// get the user's input
var wordsMade = getText("wordInput");

// loop through the letters list
for (var i = 0; i < letters. Length; i  ) {
// compare the first letter of the user's input with the random letter
if (wordsMade[0] == randomLetter) {
// uses appendItem() to add a new item to the word list
appendItem(guessedList, getText("wordInput"));
setProperty("wordInput", "text", "");
// convert the guessedList array into a string and set it as the value of the wordsOutput element
setText("wordsOutput", guessedList.join("\n"));
updateScore();
}
}
}

CodePudding user response:

var letters = ["a","b","c","d","e","f","g"];
var button = document.querySelector("button");
var resultArray = [];
button.addEventListener("click", function (evt) {
  var val = document.querySelector("input").value;
  let letter = letters[Math.floor(Math.random()*letters.length)];
  console.log("Random letter chosen was "   letter);
  if(val.charAt(0) === letter){
    resultArray.push(val);
  }
  console.log("Result list is: "   resultArray);
});
<input type="text" size="10">
<button>submit</button>

  • Related