-2

I have an array which the values are :

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26

then i want to check this values to the array :

14,15,16,17

i tried with this but didn't work :

function contains(a, toFind) {
    for (var i = 0; i < a.length; i++) {
        if (equalArray(a[i], toFind)) {
            return true;
        }
    }
    return false;
}

function equalArray(a, b) {
    if (a.length === b.length) {
        for (var i = 0; i < a.length; i++) {
            if (a[i] !== b[i]) {
                return false;
            }
        }
        return true;
    } else {
        return false;
    }
}

Anyone can help me out ?

0x00b0
  • 343
  • 1
  • 3
  • 17
  • The code you are using would only work if both were arrays of arrays. – Luca Kiebel Mar 14 '19 at 11:10
  • 3
    What does *"then i want to check this values to the array"* mean? You want to see if it has *any* of them? *All* of them? In that order? In any order? Consecutively? – T.J. Crowder Mar 14 '19 at 11:10
  • 1
    Why dont you use `indexOf` or `includes`? – brk Mar 14 '19 at 11:11
  • SOLVED [https://stackoverflow.com/questions/9204283/how-to-check-whether-multiple-values-exist-within-an-javascript-array](https://stackoverflow.com/questions/9204283/how-to-check-whether-multiple-values-exist-within-an-javascript-array) – 0x00b0 Mar 14 '19 at 11:11

1 Answers1

0

use includes.

let a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26];

console.log(a.includes(14));
console.log(a.includes(15));
console.log(a.includes(16));
console.log(a.includes(17));
Stanimirovv
  • 3,064
  • 8
  • 32
  • 56