Skip to content

Instantly share code, notes, and snippets.

@tianmingzuo
Created October 11, 2018 15:39
Show Gist options
  • Select an option

  • Save tianmingzuo/4f4fd4841307efb807ef7d711e8c8d50 to your computer and use it in GitHub Desktop.

Select an option

Save tianmingzuo/4f4fd4841307efb807ef7d711e8c8d50 to your computer and use it in GitHub Desktop.
/* You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments. */
/*
You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.
*/
function destroyer(arr) {
let len =arguments.length;
let indexArr = [];
let arrCopy = arr.concat([]); //do not change the input array
if(len > 1){
for(let i=0; i<arrCopy.length; i++){
for(let j=1; j<len; j++){
if(arrCopy[i] === arguments[j]){
indexArr.push(i);
}
}
}
}else{
return arrCopy;
}
//console.log(indexArr);
/* for(let k=0; k<indexArr.length; k++){
let temp = '';
temp = arrCopy[k];
arrCopy[k] = arrCopy[indexArr[k]];
arrCopy[indexArr[k]] = temp;
}
let newArr = arrCopy.slice(indexArr.length);
return newArr; */
for(let k=0; k<indexArr.length; k++){
let temp = arrCopy[indexArr[k]];
for(let m=indexArr[k]-1; m>=0; m--){
arrCopy[m+1] = arrCopy[m];
}
arrCopy[0] = temp;
}
let newArr = arrCopy.slice(indexArr.length);
return newArr;
}
console.log(destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3));
/*console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));
console.log(destroyer([3, 5, 1, 2, 2], 2, 3, 5));
console.log(destroyer([2, 3, 2, 3], 2, 3));
console.log(destroyer(["tree", "hamburger", 53], "tree", 53));
console.log(destroyer(["possum", "trollo", 12, "safari", "hotdog", 92, 65, "grandma", "bugati", "trojan", "yacht"],
"yacht", "possum", "trollo", "safari", "hotdog", "grandma", "bugati", "trojan"));
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment