简体   繁体   中英

Return true is string contains specific character in Javascript

Just learning JS and I'm trying to answer the following question: write a function that takes 2 inputs (string and character), uses a for loop and will return true if the character is found within the string.

Here is what I came up with:

var hasEl = function(str, char) {
  for(var i = 0; i < str.length; i++) {
    if (str[i] == char) {
      return true;
    } else {
      return false;
    }
  }
};
console.log(hasEl("I am Batman", "n"));

This returns 'false' when I run it, however, if I change the "n" to "I" it returns 'true'. So maybe it's only checking the first character of the string?

Thank you for your help.

Regards

If the 1st char is not the requested one, the if will fail, and the else block will return false immediately.

Move the return false; outside of the loop. If the char is found in the string, it will break the loop, and return true . If the loop ends without returning, it will return false .

 var hasEl = function(str, char) { for(var i = 0; i < str.length; i++) { if (str[i] == char) { return true; } } return false; }; console.log(hasEl("I am Batman", "n")); 

只需使用indexOf

"asd".indexOf('a') >= 0

Better way to do this is to use .indexOf() method available on string. It will return -1 if not found

http://www.w3schools.com/jsref/jsref_indexof.asp

Are you learning programming or JS?

If you are learning JS, you can just do

const hasEl = (str, c) => str.includes(c);

The reason your code is not working because if the condition is true, the function will just return false.

You need to do

var hasEl = function(str, char) {
    for(var i = 0; i < str.length; i++) {
        if (str[i] == char) {
            return true;
        }
    }
    return false;
};

Try This code

 var hasEl = function(str, char) { for(var i = 0; i < str.length; i++) { if (str[i] == char) { return true; } } return false; }; console.log(hasEl("I am Batman", "n")); 

or you can use this also

 var hasEl = function(str, char) { if (str.indexOf(char)!=-1) { return true; } return false; }; console.log(hasEl("I am Batman", "n")); 

The problem with your code is, that it runs the else or rather retun false statements in every round of the loop. But as soon as only one return statement gets executed, the whole function stops... So you could change your function as follows:

 var hasEl = function(str, char) { for(var i = 0; i < str.length; i++) { if (str[i] == char) { return true; } } return false; }; 

However, there is a much easier way to achive the same solution:

 var hasEl = function(str, char) { if(str.indexOf(char)==-1){ return false; }else{ return true; } }; 

It works using the build in indexOf function, which usually returns the index of a specific string (in this case only a char) inside the string the function is used as a method of. However, the function returns -1 if the char isn't found in the string. The rest should be quite self-explanatory.

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