Javascript Array isSubsetOf(contextArray)
// 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 both arrays will contain only strings. Array.prototype.isSubsetOf = function(contextArray) { let occurred = {}; contextArray.forEach((item) => occurred[item] = true); return this.reduce((result, item) => result && !!occurred[item], true) };
'use strict';/* w w w. ja v a 2s.c o m*/ //both arrays will contain only strings Array.prototype.isSubsetOf = function(contextArray) { const largerSet = new Set(); contextArray.forEach((item) => { largerSet.add(item); }); console.log(largerSet); let currentElem; for (let i = 0; i < this.length; i++) { currentElem = this[i]; if (largerSet.has(currentElem) === false) { return false; } } return true; }; const myArray = ['cat', 'dog', 'cow']; console.log(myArray.isSubsetOf(['dog', 'cow', 'fox'])); //false // console.log(myArray.isSubsetOf(['cat', 'gorilla'])); //true // console.log(myArray.isSubsetOf(['catfish'])); //false