Ide like to use an if statement to check if an input field is a value is NOT contained in an array and can’t for the life of me figure out how to do it.
this is what im trying to do
if (input.value.includes(!myArrayList[ ]) {…}
Assuming input is the input field and input.value is what the user inputs.
any ideas how to do this? I tried a few things but nothing works…
// a single value IS in some array
someArray.includes(aSingleValue)
// a single value IS NOT in some array
someArray.includes(aSingleValue) === false
// or, same thing here:
!someArray.includes(aSingleValue)
if the user inputs “bob”, I want to first check myArrayList[ ] and see if bob is an array item. If NOT, then run the code block. if Bob is a part of the array (myArrayList), then don’t run the code block.
@DanCouper Ive tried that method and I assumed it was wrong because my code block will always run. I know this because the variable I have inside the block will assign even if the conditions are not met
(searchMode = true)
if(!keyWords.includes(inputBox.value) || !colors.includes(inputBox.value)) {
q= inputBox.value;
const dontGetIt =[
"Sorry, Im having trouble understanding you.",
"I didnt quite get that.",
"I dont understand.",
"Sorry, I dont know what that means yet.",
"???",
]
mainParagraph.innerHTML = dontGetIt[Math.floor(Math.random()*dontGetIt.length)]+" "+ `Would you like me to search ${q}? Type "yes" or "no"`;
searchMode = true;
}
So it seems when I have my if conditionals like this the code inside the brackets will run anyway…
Gimme a sec to figure out what you’re doing here, but just fyi so it’s a bit easier to read through stuff:
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
Im assuming that you mean the ‘input box.value’ ? this is from a single input field.
This is basically anything I enter into the input that IS a part of the arrays.
so for example if I enter “bob”, which is a value in the array; the code block will run. and I don’t want it to. I want it to only run if what I enter into the input ISNT in the array.
those are the arrays that I am looking to check through, just like the someArray example you provided.
Yes, but if it’s always getting past that conditional regardless of what you type, then what that value is (and what the two arrays of values you’re checking against contain) becomes very important, because you are assuming one of these things is a value you expect, and it is not. If the value was in one of those arrays, the code in the block would not run, so if it is always running, then the value is not what you think it is.
I have this code. and the real thing that is the problem is the searchMode variable assignment here.
so if I enter in a value that IS in one of the arrays here (being keyWords || colors)
the code block will run and searchMode is assigned TRUE. That causes a problem in my code later down the line. I need searchMode to remain FALSE, its original setting outside of the if statement here.
But as I noted, for some reason im console.logging search mode and im seeing that after that line in my code(the line inside the if statement in question), its returning true.
Put searchMode = false directly before the if condition. If I’m understanding you correctly, then you want to always run that condition, so turn off the search mode, run the condition. Can’t see affect on rest of code so this is just a suggestion but it may do what you want
Right, so if the condition always runs, then either the value is not what you think it is, or the value is not ever one of the values present in either of those two arrays, so need to know what they are and what value is
I have a feeling the input value is a whole sentence, while the keywords and colors arrays are lists of words. @tuscannypolk is that right? If that’s the case you’d need to split the input value into words and then check if any of those words are included in the list.
haha im so confused by this. I think I get what you mean and it makes sense… but the thing that confuses me is that I don’t see how either of these could be the case. Even though the code is in fact behaving in such a way…