Comparing an array element with a string

Asked

Viewed 2,804 times

4

I need to create a function that receives a array and compare with a string, outside the array, returning true if, and only if, there is at least twice the occurrence of string outside the array. The output should be similar to 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";}
        }
    }
}

3 answers

4

Just take the falsefrom within the loop. If it arrives at the count of 2, it can terminate the execution by returning true. But if he doesn’t reach that condition, he has to keep trying to the end, he can’t return false inside the loop. If it exits the loop, it means that the count never reaches 2, then it can return false.

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

I put in the Github for future reference.

  • Saturday night if you steal my answer ;)

  • I have already tried this. When it is greater than 1, in the output "true" appears, but when the string appears only once, it does not return false.

  • @Vanessaribeiro The way I made it appear.

  • Thank you! I’ve solved it...

  • @Vanessaribeiro I know Sergio will hit but his is wrong. At least within the method you are using.

  • I understood, is that in reality ended up missing a sign of minor that limits the loop, as Sergio suggested...

  • @Vanessaribeiro is had but this mistake I did not realize. Now it’s right.

Show 2 more comments

3

The problem there is that within the first execution it checks:

a > 1 ?

But at the first run it will never be greater than 1, just like you put Else. It always falls into Else. It’s right to check this later.

Following example https://jsfiddle.net/dk86foxb/

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

2


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

But your problem is different, is that you have this loop with mistakes:

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

Would be corrected:

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: http://jsfiddle.net/9xwajvrm/1/

Note yet another possible problem, is that you are returning Strings and not Boolean. Note that "false" and false is not the same thing...

Having said that about your code I suggest you use the .filter() who does exactly what you want. So:

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

jsFiddle: http://jsfiddle.net/9xwajvrm/

  • I corrected the answer because I explained it right in the first line but had left it wrong in the code...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.