Last active
August 3, 2021 23:34
-
-
Save Dangeranger/598dd71b521e5fb59ecb9358361e310c to your computer and use it in GitHub Desktop.
Revisions
-
Dangeranger revised this gist
Aug 3, 2021 . 1 changed file with 51 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,51 @@ 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}`); -
Dangeranger revised this gist
Aug 3, 2021 . 1 changed file with 46 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,46 @@ 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}`); -
Dangeranger created this gist
Aug 3, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ 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}`);