eat a burger in javascript

There are many ways to approach this question, but let's go with a fun and humorous solution!

First, let's create an object called "burger" with properties such as "toppings", "sauce", and "isVegetarian".

index.tsx
let burger = {
  toppings: ['beef patty', 'lettuce', 'cheddar cheese', 'tomato', 'onion'],
  sauce: 'ketchup',
  isVegetarian: false
};
136 chars
6 lines

Next, let's create a function called "eatBurger" that will log a series of messages to the console as if we are eating the burger.

index.tsx
function eatBurger(burger) {
  if (burger.isVegetarian) {
    console.log("Sorry, there's no meat in this burger. You're missing out!");
  } else {
    console.log("Mmm...that " + burger.toppings[0] + " tastes delicious");
    console.log("Oh man, this " + burger.toppings[1] + " is so fresh and crispy");
    console.log("I can't get enough of this gooey " + burger.toppings[2]);
    console.log("The " + burger.toppings[3] + " adds the perfect amount of sweetness");
    console.log("And finally, this " + burger.toppings[4] + " really brings everything together.");
    console.log("Time to go back for seconds!");
  }
}
624 chars
13 lines

Finally, let's call the "eatBurger" function with our "burger" object as the argument, and watch as the console displays our faux eating experience.

index.tsx
eatBurger(burger);
19 chars
2 lines

Output:

index.tsx
Mmm...that beef patty tastes delicious
Oh man, this lettuce is so fresh and crispy
I can't get enough of this gooey cheddar cheese
The tomato adds the perfect amount of sweetness
And finally, this onion really brings everything together.
Time to go back for seconds!
267 chars
7 lines

Of course, this is just a silly example of what you can do with JavaScript. The possibilities are endless!

gistlibby LogSnag