简体   繁体   中英

Find words based on what the word starts and ends with - Javascript (code.org)

So I'm trying to create code that can find words based on what the word starts and ends with, and how many characters does it have. I'm creating this for a project in code.org, and I'm not sure if this is common to see in a question. The language is JavaScript I think.

//listWord is the data of words
//findlistWord is the 'default setting' for the output for now
var listWord = getColumn("Words", "Word");
var findlistWord = [];

//find is the function for receiving the data a person inputs
//find1 is the function that refreshes the data every time
//letterCount grabs the amount of letters you choose in the dropdown
//startInput grabs the text of what the word will start with
//endInput grabs the text of what the word will end with
function find1() {
  find(getNumber("letterCount"), getText("startInput").toLowerCase(), getText("endInput").toLowerCase());
}

//onEvents for when a person changes the data; refreshes everything with find1 function
onEvent("letterCount", "change", function() {
  find1();
});

onEvent("startInput", "input", function() {
  find1();
});

onEvent("endInput", "input", function() {
  find1();
});

//len, one, two = letterCount, startInput, endInput
function find(len, one, two) {
  findlistWord = [];
  setText("wordOutput", "");

  //Loop that puts the extracted letters into position, finding words
  //listWord is the variable of all the words in the code.org data table
  for (var i = 0; i < listWord.length; i++) {
    if (listWord[i].length == len && listWord[i].substring(0, one.length) == one && listWord[i].substring(len.length - two.length - 1, len.length) == two) {
      appendItem(findlistWord, listWord[i]);
    }
  }

  //Alternative for if no results show
  if (findlistWord.length == 0) {
    appendItem(findlistWord, "There were no words that fit the limitations!");
  }

  //Set the output results based on data collected, combining them with a ", "
  setText("wordOutput", findlistWord.join(", "));

}

At the moment I'm having problem making what the word ends with code work. Not sure how to make the substring work.

So I'm trying to create code that can find words based on what the word starts and ends with, and how many characters does it have. I'm creating this for a project in code.org, and I'm not sure if this is common to see in a question. The language is JavaScript I think.

//listWord is the data of words
//findlistWord is the 'default setting' for the output for now
var listWord = getColumn("Words", "Word");
var findlistWord = [];

//find is the function for receiving the data a person inputs
//find1 is the function that refreshes the data every time
//letterCount grabs the amount of letters you choose in the dropdown
//startInput grabs the text of what the word will start with
//endInput grabs the text of what the word will end with
function find1() {
  find(getNumber("letterCount"), getText("startInput").toLowerCase(), getText("endInput").toLowerCase());
}

//onEvents for when a person changes the data; refreshes everything with find1 function
onEvent("letterCount", "change", function() {
  find1();
});

onEvent("startInput", "input", function() {
  find1();
});

onEvent("endInput", "input", function() {
  find1();
});

//len, one, two = letterCount, startInput, endInput
function find(len, one, two) {
  findlistWord = [];
  setText("wordOutput", "");

  //Loop that puts the extracted letters into position, finding words
  //listWord is the variable of all the words in the code.org data table
  for (var i = 0; i < listWord.length; i++) {
    if (listWord[i].length == len && listWord[i].substring(0, one.length) == one && listWord[i].substring(len.length - two.length - 1, len.length) == two) {
      appendItem(findlistWord, listWord[i]);
    }
  }

  //Alternative for if no results show
  if (findlistWord.length == 0) {
    appendItem(findlistWord, "There were no words that fit the limitations!");
  }

  //Set the output results based on data collected, combining them with a ", "
  setText("wordOutput", findlistWord.join(", "));

}

At the moment I'm having problem making what the word ends with code work. Not sure how to make the substring work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM