eat a sandwich in javascript

To simulate eating a sandwich in JavaScript, you can create an object that represents the sandwich, with properties such as "breadType", "toppings", and "sauce". You can then use methods on this object to simulate the act of eating the sandwich, such as "removeToppings()" to remove some of the toppings, or "consume()" to signify the sandwich has been eaten.

Here's some example code:

index.tsx
class Sandwich {
  constructor(breadType, toppings, sauce) {
    this.breadType = breadType;
    this.toppings = toppings;
    this.sauce = sauce;
  }

  removeToppings(n) {
    this.toppings.splice(0, n);
  }

  consume() {
    console.log("Nom nom nom... Delicious sandwich has been eaten.");
    this.toppings = [];
    this.sauce = null;
    this.breadType = null;
  }
}

const mySandwich = new Sandwich("wheat", ["ham", "cheese", "lettuce", "tomato"], "mayo");
console.log("My sandwich before eating:", mySandwich);

mySandwich.removeToppings(2);
console.log("My sandwich after removing first 2 toppings:", mySandwich);

mySandwich.consume();
console.log("My sandwich after eating:", mySandwich);
702 chars
28 lines

This code creates a new Sandwich object, removes some toppings from it, and then "eats" the sandwich by calling the consume() method. The output will be:

index.tsx
My sandwich before eating: Sandwich {
  breadType: 'wheat',
  toppings: [ 'ham', 'cheese', 'lettuce', 'tomato' ],
  sauce: 'mayo'
}
My sandwich after removing first 2 toppings: Sandwich {
  breadType: 'wheat',
  toppings: [ 'lettuce', 'tomato' ],
  sauce: 'mayo'
}
Nom nom nom... Delicious sandwich has been eaten.
My sandwich after eating: Sandwich {
  breadType: null,
  toppings: [],
  sauce: null
}
403 chars
17 lines

gistlibby LogSnag