Javascript Array Is Array a subset of another array

Introduction

Make an array method that can return whether or not a context array is a subset of an input array.

To simplify the problem, you can assume that neither array will contain objects or arrays as elements within them.

var a = ['commit','push']
a.isSubsetOf(['commit','rebase','push','blame']) // true

Array.prototype.isSubsetOf = function (arr) {
  // your code here
  let result = 0;
  for (let i = 0; i < this.length; i += 1) {
    for (let p = 0; p < arr.length; p += 1) {
      if (this[i] === arr[p]) {
        result += 1;/*  ww  w .  j av  a2 s . co m*/
        break;
      }
    }
    if (i + 1 !== result) {
      return false;
    }
  }
  return true;
};
const b = ['merge','reset','reset']
console.log(b.isSubsetOf(['reset','merge','add','commit']));
const a = ['commit','push','blame'];
console.log(a.isSubsetOf(['commit','rebase','push','blame']));



PreviousNext

Related