Skip to content

Instantly share code, notes, and snippets.

@Dangeranger
Last active August 3, 2021 23:34
Show Gist options
  • Select an option

  • Save Dangeranger/598dd71b521e5fb59ecb9358361e310c to your computer and use it in GitHub Desktop.

Select an option

Save Dangeranger/598dd71b521e5fb59ecb9358361e310c to your computer and use it in GitHub Desktop.

Revisions

  1. Dangeranger revised this gist Aug 3, 2021. 1 changed file with 51 additions and 0 deletions.
    51 changes: 51 additions & 0 deletions moveItemDebug.js
    Original 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}`);
  2. Dangeranger revised this gist Aug 3, 2021. 1 changed file with 46 additions and 0 deletions.
    46 changes: 46 additions & 0 deletions moveItemWithoutDuplicates.js
    Original 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}`);
  3. Dangeranger created this gist Aug 3, 2021.
    37 changes: 37 additions & 0 deletions moveItemFromArray.js
    Original 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}`);