Skip to content

Instantly share code, notes, and snippets.

@Dangeranger
Last active August 3, 2021 23:34
Show Gist options
  • Save Dangeranger/598dd71b521e5fb59ecb9358361e310c to your computer and use it in GitHub Desktop.
Save Dangeranger/598dd71b521e5fb59ecb9358361e310c to your computer and use it in GitHub Desktop.
An example of moving an item from one Array to another Array
let dinnerFood = ['salad', 'soup', 'casserole'];
let breakfastFood = ['omelet', 'pancakes', 'fruit'];
console.log(`From array: ${breakfastFood}`);
console.log(`To array: ${dinnerFood}`);
function moveItem(itemToMove , fromArray, toArray) {
function extractItem() {
// we know the item is in the starting array
function isItem(currentItem) {
// if the two items are the same
// then return the item to the caller
return itemToMove === currentItem;
}
let theItem = fromArray.find(isItem);
// ['salad', 'soup', 'casserole']
// ['salad', 'casserole']
// let start = dinnerFood.slice(0, 1) => ['salad']
// let end = dinnerFood.slice(2) => ['casserole']
let index = fromArray.indexOf(itemToMove);
let start = fromArray.slice(0, index);
let end = fromArray.slice(index + 1);
console.log(start, end);
let result = start.concat(end);
console.log(result);
fromArray = result;
return theItem;
}
if (fromArray.includes(itemToMove) && !toArray.includes(itemToMove)) {
console.log("FIRST RUNNING")
let theItem = extractItem(fromArray);
toArray.push(theItem);
return true
} else if (fromArray.includes(itemToMove)) {
console.log("SECOND RUNNING");
extractItem(fromArray);
}
return false;
}
moveItem("pancakes", breakfastFood, dinnerFood);
moveItem("pancakes", breakfastFood, dinnerFood);
console.log(`From array: ${breakfastFood}`);
console.log(`To array: ${dinnerFood}`);
let dinnerFood = ['salad', 'soup', 'casserole'];
let breakfastFood = ['omelet', 'pancakes', 'fruit'];
console.log(`From array: ${breakfastFood}`);
console.log(`To array: ${dinnerFood}`);
function moveItem(itemToMove , fromArray, toArray) {
if (fromArray.includes(itemToMove) && !toArray.includes(itemToMove)) {
// we know the item is in the starting array
function isItem(currentItem) {
// if the two items are the same
// then return the item to the caller
return itemToMove === currentItem;
}
let theItem = fromArray.find(isItem);
// ['salad', 'soup', 'casserole']
// ['salad', 'casserole']
// let start = dinnerFood.slice(0, 1) => ['salad']
// let end = dinnerFood.slice(2) => ['casserole']
let index = fromArray.indexOf(itemToMove);
let start = fromArray.slice(0, index);
let end = fromArray.slice(index + 1);
fromArray = start.concat(end);
toArray.push(theItem);
return true
}
return false;
}
moveItem('pancakes', breakfastFood, dinnerFood);
console.log(`From array: ${breakfastFood}`);
console.log(`To array: ${dinnerFood}`);
let dinnerFood = ['salad', 'soup', 'casserole'];
let breakfastFood = ['omelet', 'pancakes', 'fruit'];
console.log(`From array: ${breakfastFood}`);
console.log(`To array: ${dinnerFood}`);
function moveItem(itemToMove , fromArray, toArray) {
function extractItem() {
// we know the item is in the starting array
function isItem(currentItem) {
// if the two items are the same
// then return the item to the caller
return itemToMove === currentItem;
}
let theItem = fromArray.find(isItem);
// ['salad', 'soup', 'casserole']
// ['salad', 'casserole']
// let start = dinnerFood.slice(0, 1) => ['salad']
// let end = dinnerFood.slice(2) => ['casserole']
let index = fromArray.indexOf(itemToMove);
let start = fromArray.slice(0, index);
let end = fromArray.slice(index + 1);
fromArray = start.concat(end);
return theItem;
}
if (fromArray.includes(itemToMove) && !toArray.includes(itemToMove)) {
let theItem = extractItem();
toArray.push(theItem);
return true
} else if (fromArray.includes(itemToMove)) {
extractItem();
}
return false;
}
moveItem("pancakes", breakfastFood, dinnerFood);
moveItem("pancakes", breakfastFood, dinnerFood);
console.log(`From array: ${breakfastFood}`);
console.log(`To array: ${dinnerFood}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment