javascript - check if string is in a array without case sensitive [duplicate] - javascript

This question already has answers here:
How to check if a string array contains one string in JavaScript? [duplicate]
(5 answers)
Closed 7 years ago.
I want to make a filter. Where if you input a word that is in the 'blacklist' it will tell something. I've got all the code but have a problem.
JS:
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (input == array)
// I will do something.
} else {
// Something too
}
}
I want to make it so that if the input is a item in the array. That the statement is true. But what is the correct way to do this? Because what I'm doing here doesn't work! Also I want to get rid of the case sensitive! So that if the array has hello in it both hello and Hello are detected.
Sorry if this question is asked before. I searched for it but didn't know what keywords to use.
EDIT 1:
I am changing my question a little bit:
I want to check what is in my original question but with some other features.
I also want to check if input has a part of an item in array. So that if the input is hello that helloworld is being detected because is has hello in it. As well as hello or Hello.

Use indexOf:
if (array.indexOf(input) > -1)
It will be -1 if the element is not contained within the array.

This code should work:
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (array.indexOf(input) >= 0)
// I will do something.
} else {
// Something too
}
}
The indexOf Method is member of the array type and returns the index (beginning at 0) of the searched element or -1 if the element was not found.

I think what you are looking for is
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (array.indexOf(input) !== -1 )
// I will do something.
} else {
// Something too
}
}

Related

How to use a variable in a Jquery selector [duplicate]

This question already has answers here:
jQuery selectors with variables
(5 answers)
Closed 2 years ago.
I want to be able to use a variable inside a Jquery selector:
I have 100 values on screen, called myValue11, myValue21, myValue31 etc, and I want to be able to insert data into one of these values, based on its value
var myVariable;
//Checking if the values are empty, if so, set 'myVariable' to a numeric value
if ($('#myValue11').val() == "")
{
myVariable = 1;
}
else if ($('#myValue21').val() == "")
{
myVariable = 2;
}
else if ($('#myValue31').val() == "")
{
myVariable = 3;
}
//inserting into the first empty value, by using 'myVariable'
$('#myValue+myVariable+1').val("Hello World!"); //Doesn't work
The code above doesn't work, I can't seem to find a way to insert this variable into the selector, any help would be appreciated. Thank you
Try
$('#myValue'+myVariable+'1').val("Hello World!");
You needed the extra quotes to terminate the strings.

New to coding, What Am I missing? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
working on JS, What am I missing? Thank you
Modify the function below to greet only those with an even number of letters in their name
function helloYou(name)
numbers.filter (n => n % 2 =i= 1);{
}
/* Do not modify code below this line */
console.log(helloYou('Bob'), `<-- should return undefined`)
console.log(helloYou('Anna'), `<-- should return "Hello, Anna!"`)
To access the number of letters in the string, you can use the attribute .length.
Then to check if this number is even a modulus 2 should return 0, that's what we need to check. This condition goes in an if statement.
Finally, if this condition is met, return Hello concatenated with name.
Otherwise nothing is returned, so it's undefined (there is no need to explicitly write return undefined).
function helloYou(name) {
if (name.length % 2 === 0) {
return "Hello, " + name;
}
}
/* Do not modify code below this line */
console.log(helloYou('Bob'), `<-- should return undefined`)
console.log(helloYou('Anna'), `<-- should return "Hello, Anna!"`)
The variable numbers is actually undefined in this case.
Also, filter is not useful in this situation. Filter is mainly used to get the elements that match a condition from an array.
Your should use an if statement to check for even length. Better yet, you can use the ternary operator. Here is an example:
function helloYou(name) {
return name.length % 2 === 0 ? 'Hello, ' + name : undefined;
}
console.log(helloYou('Bob'));
console.log(helloYou('Anna'));

How do I create an if statement that sets an individual variable equal to at least one of my array elements [duplicate]

This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 3 years ago.
I have a line of code that is supposed to act as a check for if the user inputs a value that isn't within my array.
I made an array with all valid inputs and then made and if statement that checks this.
var productCode = ["LT","ST","DC","LC","PR","SP"];
var productChosen = prompt("Choose a product code, LT, ST, DC, LC, PR, or SP");
if ( productChosen === productCode) {
etc..
}
else {
alert("Please input a valid product code");
}
It always goes to the else statement.
The productChosen is a string. The productCode is an array. These types can never be equal. What you are looking for is includes() to check if the string is included in the array.
if(productCode.includes(productChosen)) { ... }

Javascript For Loop...One function in another? [duplicate]

This question already has answers here:
How to count vowels in a Javascript string with two functions?
(5 answers)
Closed 6 years ago.
Alright I cant seem to get this to work. I have one function written so far. It works great. Now I need to get my new function to call the second function.
Here is the first function:
function isVowelR(str) {
if(str = str.match(/[aeiou]/gi))
return true
else
return false
}
This one works fine. Returns true if str is a vowel. Here is the one Im stuck on though. This is what Ive tried (as well as some other stuff.
function countVowels(str) {
var count = 0
for (var i = 0; i == str.length; i++)
{
if (i == isVowelR(i))
{
++count
}
}
return count
}
This second function needs count how many vowels are in the string that is entered (which I figured out in a different function). But how do I get it to work if I am required to call the first function?
The problem is that the i variable is a number, so what you actually do is isVowelR(0), isVowelR(1), isVowelR(2) ...
You can change this to:
isVowelR(str[i])
Another option is just to check the length of the match:
str.match(/[aeiou]/gi).length
This way you don't need to loop over each character on the string:
function countVowels(str) {
return str.match(/[aeiou]/gi).length
}

finding a index of string [duplicate]

This question already has answers here:
How to check if the URL contains a given string?
(21 answers)
Closed 6 years ago.
I have a long array of strings with page IDs; when the current page ID matches one from the array, stuff needs to happen (alert in this test).
The alert pops up on any pages, regardless if the url contains one of the ids from the array or not. What is wrong with my if statement: if(pageHref.indexOf(id))? thanks for any advise
var pageHref = window.location.href;
var ids = ['14528','14417','17529'];
for (var i = 0; i < ids.length; i++) {
var id = ids[i];
if (id.length > 0) {
if(pageHref.indexOf(id)){
//do something
alert('a');
}
}
}
A blog post here has a table that explains what values evaluate as true in javascript conditional statements. For numerical values it states only +0,-0 and NaN evaluate to false. Because indexOf returns -1 for no match, it is evaluating to true for non-matching ids. You would want to use:
if(pageHref.indexOf(id) > -1)
IndexOf returns a number, if the number is -1, there is no match.
Try for example.
pageHref.indexOf(id) != -1.

Categories

Resources