-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 ?

4
  • The code you are using would only work if both were arrays of arrays. Commented Mar 14, 2019 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? Commented Mar 14, 2019 at 11:10
  • 1
    Why dont you use indexOf or includes? Commented Mar 14, 2019 at 11:11
  • SOLVED https://stackoverflow.com/questions/9204283/how-to-check-whether-multiple-values-exist-within-an-javascript-array Commented Mar 14, 2019 at 11:11

1 Answer 1

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));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.