Remove all elements from the initial array that are of the same value as the arguments:
function destroyer(arr) {
for(var j=1; j<arguments.length; j++){
arr = arr.filter(checkVal, arguments[j]);
}
return arr;
}
function checkVal(value){
return (value != this);
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3) should return [1, 1].
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3) should return [1, 5, 1].
destroyer([3, 5, 1, 2, 2], 2, 3, 5) should return [1].
destroyer([2, 3, 2, 3], 2, 3) should return[].
destroyer(["tree", "hamburger", 53], "tree", 53) should return ["hamburger"].
Return Largest Number in an Array:
function largestOfFour(arr) {
var resultArr=[];
for(var i=0; i<arr.length;i++){
var maxInt=0;
for(var j=0; j<arr[i].length; j++){
maxInt = Math.max(maxInt, arr[i][j]);
}
resultArr[i] = maxInt;
}
return resultArr;
}
Find Index to Insert an Item in an Array:
function getIndexToIns(arr, num) {
arr = arr.sort(function(a,b){
return a>b;
});
var i=0;
while(i<arr.length && arr[i]<num){
i++;
}
return i;
}
Find all items that exist in either one of two arrays:
function diffArray(arr1, arr2) {
var newArr = [];
var newArr2 = [];
newArr = arr1.filter(function(elem, index, array) {
for(var i=0; i<arr2.length; i++){
if(elem==arr2[i]) return false;
}
return true;
});
newArr2 = arr2.filter(function(elem, index, array) {
for(var i=0; i<arr1.length; i++){
if(elem==arr1[i]) return false;
}
return true;
});
return newArr.concat(newArr2);
}
Leave a comment