I am trying to filter an array (like ["hello", "hi", "bye"]
) for a specific character (like "h") so that it returns a new array containing only Strings that have that character (["hello", "hi", "bye"]
-> ["hello", "hi"]
).
Here is my current code. giveSOFT
returns []
for some reason.
function giveSOFT(input, char) {
var length = input.length
var sublist = []
var word = ""
var w = "piss"
for (let i = 0; i < input.length; i++) {
word = input[i]
for (let b = 0; b < word.length; b++) {
var input = word[b]
var n = input.includes(w)
if (n === true) {
console.log(input)
}
return sublist
}
}
}
var input = ["piss", "wii", "ww", "iiwi", "iii", "piss"]
var char = "i"
console.log(giveSOFT(input, char));
I don't understand why or how to fix this so any advice would be appreciated.
Here is my exact instructions on what I am trying to do:
Create a function that takes two arguments: a list of words and a letter. The function should create and return a sublist of the list in the argument. The sublist should contain only those words from the original list which contain the letter passed as the second argument. Use .indexOf() to find if the letter passed as an argument is in the word, if it is - push it to a new list, if not - skip