Comparing an array element with a string

4

I need to create a function that gets an array and compare it to a string , outside the array , returning true if, and only if, at least twice the string exists outside the array . The output should look like this:

Str2(["a", "b", "a", "c"], "c");
//=> "false"

Str2(["a", "b", "a", "c"], "a");
//=> "true"

The function below should do this, but always returns false ".

var Str2 = function(strs, str){
    var i, a = 0;

    for(i=0; strs.length; i++){
        if (str == strs[i] ) {
            a = a + 1;
            if (a > 1) {return "true";}
            else {return "false";}
        }
    }
}
    
asked by anonymous 10.10.2015 / 23:38

3 answers

2

I think in your logic return false should be at the end of the loop, because only then will you know there are no repetitions.

But your problem is another, you have this loop with errors:

# 1 - Should be i < strs.length; and not only ; length; . # 2 - The first iteration will have i with value 1 and this will false in i > 1) then go to else ...

Fixed would be:

var Str2 = function (strs, str) {
    var i, a = 0;
    for (i = 0; i < strs.length; i++) {
        if (str == strs[i]) {
            a++;
            if (a > 1) return "true";
        }
    }
    return "false";
}

jsFiddle: link

Also note another possible problem, you are returning Strings and not Booleans . Note that "false" and false is not the same thing ...

Having said this about your code I suggest you use .filter() which does exactly what you want. So:

var Str2 = function(strs, str){
    return strs.filter(function(el){
        return el == str;
    }).length > 1;
}

jsFiddle: link

    
10.10.2015 / 23:49
4

Just take the false from within the loop. If it reaches the count of 2, it can quit execution by returning true . But if it does not reach this condition, it has to stay trying until the end, it can not return false within the loop. If it exits the loop, it means that the count never reaches 2, so it can return false .

var Str2 = function(strs, str){    
    var i, a = 0;	
    for(i=0; i < strs.length; i++){        
        if (str == strs[i] ) {
            a = a + 1;                       
            if (a > 1) {
                return "true";
            } 
        }
    }
    return 'false';
}

document.body.innerHTML += "<br>" + Str2(["a", "b", "a", "c"], "c");
document.body.innerHTML += "<br>" + Str2(["a", "b", "a", "c"], "a");
    
10.10.2015 / 23:44
3

The problem here is that within the first run it checks:

a > 1 ?

But in the first run it will never be greater than 1 as soon as you put the else. He always falls into the else. It's okay to check this out later.

Follow the example link

var Str2 = function(strs, str){    
    var i, a = 0;   
    for(i=0; i < strs.length; i++){        
        if (str == strs[i] ) {
            a = a + 1;                       
        }
        if (a > 1) {
            return "true";
        } 
    }
    return 'false';
}
    
10.10.2015 / 23:46