0

this is super similar to this question: How to check whether multiple values exist within an Javascript array

basically I need to take an array and say does it contain the following 2 value in no particular order

But

I can't use any external libraries like j.query because it's a school assignment. I know you don't want to do my assignment for me i just need this small help to answer a larger question (the only reason I put this paragraph in is because one of my other questions didn't get answers because everyone said: "it's your school work, you do it" thats nice and good but i dont know where to start on this one so yeah...)

thanks in advance.

Fane

Community
  • 1
  • 1
user6277174
  • 21
  • 1
  • 4
  • 1
    please add some use cases, the wanted result and what you have tried. please have a look here, too: [mcve] – Nina Scholz Sep 01 '16 at 13:50
  • First step here: ask your teacher. You don't want them seeing this question, especially without any sort of attempt posted. I got in trouble in college a few years back because somebody gave me my answer and I *had* tried a solution. – Sterling Archer Sep 01 '16 at 13:51
  • 1
    "but i dont know where to start", if it wasn't a program, if your teacher gave you two lists *(you know, paper)*, and told you to check whether all items from one list are contained in the other one, how would you do/approach that? – Thomas Sep 01 '16 at 13:54
  • Start here : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype :) – Brian Peacock Sep 01 '16 at 13:58
  • If you're a student programmer then I'd suggest you start with a basic `for` loop, and check each item in the array. Note also that the question you linked to has a couple of answers that don't use jQuery or other libraries. – nnnnnn Sep 04 '16 at 00:39

1 Answers1

1

if you need only a function

var arr = [1,2,3,4]

contains(2,4, arr) // true

function contains(a, b, arr){
  return arr.indexOf(a) > -1 && arr.indexOf(b) > -1
}

or you can add a prototype as well

Array.prototype.containsTwo = function(a, b){
  return this.indexOf(a) > -1 && this.indexOf(b) > -1
}

[1,2,3].containsTwo(1,3) // true

if you want to check multiple values not just two

Array.prototype.containsLot = function(){
  arguments.every(function(v){
    return this.indexOf(v) > -1
  })
}

[1,22,33,321,41,4].containsLot(1,33,22,4) // true
[1,22,33,321,41,4].containsLot(1,22,33,9) // false
lacexd
  • 903
  • 1
  • 7
  • 22