Answer the question
In order to leave comments, you need to log in
How to implement the inArray function?
Good evening everyone! I'm still quite new to JS. What are prototypes and classes, I learned only yesterday. Please help me solve the problem, which is probably the simplest for you :)
It is required to implement a function, examples of which are presented below:
inArray(15, [1, 10, 145, 8]) === false;
[23, 674, 4, 12].inArray(4) === true;
function inArray(value, array) {
for (var i = 0; i < array.length; i++) {
if (value === array[i]) {
return true;
}
}
return false;
}
inArray(15, [1, 10, 145, 8]); // false
Answer the question
In order to leave comments, you need to log in
function inArray(value, array) {
if (!array)
array = this;
for (var i = 0; i < array.length; i++) {
if (value === array[i]) {
return true;
}
}
return false;
}
Array.prototype.inArray = inArray;
console.log([1, 10, 145, 8].inArray(10));
console.log(inArray(100, [1, 10, 145, 8]));
Array.prototype.inArray = function (item) {return this.indexOf(item) > -1}
indexOf()
, there is no need to reinvent the wheel.
function inArray(key, arr) {
return arr.map(i => i === key).length > 0
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question