Skip to content

Instantly share code, notes, and snippets.

@nikita-rudenko
Created May 6, 2020 07:55
Show Gist options
  • Select an option

  • Save nikita-rudenko/dad7bd14dd1a2328b2c859d0c392d8a9 to your computer and use it in GitHub Desktop.

Select an option

Save nikita-rudenko/dad7bd14dd1a2328b2c859d0c392d8a9 to your computer and use it in GitHub Desktop.

Revisions

  1. nikita-rudenko created this gist May 6, 2020.
    47 changes: 47 additions & 0 deletions confusing.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    const cuisinesTemplate = {
    american: [],
    italian: [],
    french: []
    }

    function serveBuffet(dishes) {
    const buffet = dishes.reduce((acc, dish) => {
    const cuisine = dish.cuisine.toLowerCase();

    if (acc[cuisine]) acc[cuisine].push(dish);
    else acc[cuisine] = [dish];

    return acc;
    }, {...cuisinesTemplate});

    return buffet;
    }

    const dishes = [
    {
    cuisine: "American",
    name: "Burger",
    },
    {
    cuisine: "Italian",
    name: "Pizza",
    },
    {
    cuisine: "French",
    name: "Baguette",
    },
    {
    cuisine: "Italian",
    name: "Spaghetti",
    },
    {
    cuisine: "French",
    name: "Ratatouille",
    }
    ]

    serveBuffet(dishes);
    serveBuffet(dishes);
    serveBuffet(dishes);

    console.log(cuisinesTemplate);