Answer the question
In order to leave comments, you need to log in
How to check an array?
Hello.
How to check an array for the presence of elements in this way:
var a = ["as", "sa", "ds"];
myFunc("sa", "as", "ds"); -> 1
myFunc("sa", "as", "k"); -> 0
myFunc("sa", "as", "ds", "sa", "as", "ds"); -> 2
Answer the question
In order to leave comments, you need to log in
var a = ["as", "sa", "ds"];
function myFunc() {
var results = [];
for(var i in a) {
results.push(0);
}
for(var i = 0; i < arguments.length; i++) {
var index = a.indexOf(arguments[i]);
if(index != -1) {
results[index] += 1;
}
}
return Math.min.apply(Math, results);
}
myFunc("sa", "as", "ds"); // 1
myFunc("sa", "as", "k"); // 0
myFunc("sa", "as", "ds", "sa", "as", "ds"); // 2
myFunc("sa", "as", "ds", "sa", "as", "ks");// 1
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question