finding a index of string [duplicate] - javascript

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.

Related

Conitnuous looping for while loop [duplicate]

This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
How to find if an array contains a specific string in JavaScript/jQuery?
(7 answers)
Closed 7 months ago.
while (true) {
if (memberName == subMemberGroup.indexOf("Leonardo", "Catherine", "Luther", "Bruce", "Amy") !== -1) {
console.log("\nMember's name exists in database. Please enter a new name.");
var memberName = input.question("Please enter member's name: ");
} else break;
}
I am trying to loop through to see if "memberName" that the user has input matches any of the following strings in the "subMemberGroup" array.
However, when I tried using while loop, it keeps saying that Member's name exist even thought the "memberName" that the user input does not match the strings in "subMemberGroup" array. I tried using if statement with a break but it still does not work.
The reason why I used while loop is because I want my code to re-prompt the user the enter a new name if the "memberName" matches the strings in "subMemberGroup" array.

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)) { ... }

Find unique prefix and remove from array of string [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 4 years ago.
Improve this question
In Javascript, given an array of strings, how to find the unique prefix of all the strings and then remove that prefix from each string.
For example:
["05098701", "05012302", "0545621", "0509301"]
unique prefix would be "05"
resultant array would be
["098701", "012302", "45621", "09301"]
You need to search like a human does: check with one char, then with two and so on..
Then just remove the prefix from every item from the array.
You can do this using map method by passing a callback function.
array = ["05098701", "05012302", "0545621", "0509301"]
function longestCommonPrefix(arr){
// sort() method arranges array elements alphabetically
var sortArr = arr.sort();
// Get first array element
var arrFirstElem = arr[0];
// Get the last array element length minus one
var arrLastElem = sortArr[sortArr.length - 1];
// Get first array element length
var arrFirstElemLength = arrFirstElem.length;
// Set "i" incrementer to 0
var i= 0;
// while "i" is less than the length of the first array element AND
// the first array element character position matches the last array character position
// increment "i" by one
while(i < arrFirstElemLength && arrFirstElem.charAt(i) === arrLastElem.charAt(i)) {
i++;
}
//return the prefix
return arrFirstElem.substring(0, i);
}
prefix = longestCommonPrefix(array);
array = array.map(function(item){
return item.substring(prefix.length, item.length);
});
console.log(array);

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
}

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

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
}
}

Categories

Resources