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";}
}
}
}
Saturday night if you steal my answer ;)
– Guerra
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.
– Van Ribeiro
@Vanessaribeiro The way I made it appear.
– Maniero
Thank you! I’ve solved it...
– Van Ribeiro
@Vanessaribeiro I know Sergio will hit but his is wrong. At least within the method you are using.
– Maniero
I understood, is that in reality ended up missing a sign of minor that limits the loop, as Sergio suggested...
– Van Ribeiro
@Vanessaribeiro is had but this mistake I did not realize. Now it’s right.
– Maniero