The Virtual Cafe
An exercise in logic


Our virtual cafe has a very limited menu. All we server to drink is Coffee, Tea, or Water. And all we have to eat are Donuts, Biscuits, and Bread.

To compensate for this limited menu, we give you a free donut with you coffee, or a free biscuit with your tea. If you just want water all you will get is bread.

Below is an very "logical" example JavaScript that will take your order.



var Thirsty = confirm("Do you want something to drink?");
var Hungry = confirm("Do you want something to eat?");

if (Thirsty)
 {var Coffee = confirm("Do you want a coffee?");
   if (!Coffee) var Tea = confirm("Do you want a tea?");
 }

if (!Thirsty && Hungry)
  {var Donut = confirm("Do you want a donut?");
   var Biscuit = confirm("Do you want a Biscuit?");}

// Give the customer his order

if (!(Thirsty || Hungry))
   document.write("You get nothing");

if (Thirsty)
 {if (Hungry)
   {if (Coffee) document.write("You get a coffee and donut")
    else if (Tea) document.write("You get a tea and biscuit")
    else document.write("You get bread and water");}
   else
     {if (Coffee) document.write("You get a coffee")
      else if (Tea) document.write("You get tea")
      else document.write("You get water");}
   }

if (!Thirsty && Hungry)
   if (Donut) document.write("You get a donut")
   else if (Biscuit) document.write("You get a biscuit")
   else document.write("You get bread");


Run the program

Notes:

There are easier ways of dealing with this type of problem.

Be very careful when using nested if's especially if you are lazy and do not use the { } to block your commands. A single ; will end all your ifs.

Back