Skip to content

Instantly share code, notes, and snippets.

@Xetera
Last active December 12, 2017 07:10
Show Gist options
  • Save Xetera/3aedb59f5b5984572a369161fb40c5cd to your computer and use it in GitHub Desktop.
Save Xetera/3aedb59f5b5984572a369161fb40c5cd to your computer and use it in GitHub Desktop.
Here's a quick way of telling if the pizza you enjoy has actual toppings in it. Chances are it doesn't and you're too scared to try anything that tastes anything even slightly different to what you normally eat.
class Pizza {
    constructor(ingredients) {
        this.ingredients = ingredients;
    }
    // is this thing in the pizza a topping?
    isTopping(topping){
        let newPizza = new Pizza(this.ingredients);
        let index = newPizza.ingredients.indexOf(topping);
        if (index === -1)
            throw new Error(`${this.constructor.name} does not have ${topping} as an ingredient`);
        newPizza.ingredients.splice(index, 1);
        return newPizza.isPizza()
    }

    isPizza() {
        return this._contains(['dough', 'sauce', 'cheese'])
    }

    _contains(things){
        let found = 0;
        for (let x of things){
            for(let i of this.ingredients){
                if (x === i){
                    found++;
                }
            }
        }
        return found === things.length;
    }
}

let margarita = new Pizza(["dough", "cheese", "sauce"]);
console.log(margarita.isPizza()); // true
console.log(margarita.isTopping('cheese')); // false

let capricciosa = new Pizza(["dough", "cheese", "sauce", "ham", "olives", "mushrooms"]);
console.log(capricciosa.isPizza()); // true
console.log(capricciosa.isTopping("ham")); // true

let grilledCheese = new Pizza(["dough", "cheese"]);
console.log(grilledCheese.isPizza()); // false
console.log(grilledCheese.isTopping('cheese')); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment