Javascript tips for developing a random breakfast/lunch/dinner menu from a list of predefined foods
68What we want
Ok so the question is how to make a javascript program to make a random dinner menu so I modified my Random Page Generator code to answer this question. You can see the Random Page Generator working on my site Supreme Games.
I've explained all the code on the Hub about the Random Page generator so theres no point me repeating myself, have a look there if you want a description of what each sentence does.
My solution to the question.
<HEAD>
<SCRIPT language="JavaScript">
<!--
function get_random()
{
var ranNum= Math.floor(Math.random()*5);
return ranNum;
}
function getaStarter()
{
var whichstarter=get_random();
var starter=new Array(5)
starter[0]= "starter 1";
starter[1]= "starter 2";
starter[2]= "starter 3";
starter[3]= "starter 4";
starter[4]= "starter 5";
alert(starter[whichstarter]);
}
function getaMeal()
{
var whichmeal=get_random();
var Meal=new Array(5)
Meal[0]= "Meal 1";
Meal[1]= "Meal 2";
Meal[2]= "Meal 3";
Meal[3]= "Meal 4";
Meal[4]= "Meal 5";
alert(Meal[whichmeal]);
}
function getaDesert()
{
var whichDesert=get_random();
var Desert=new Array(5)
Desert[0]= "Desert 1";
Desert[1]= "Desert 2";
Desert[2]= "Desert 3";
Desert[3]= "Desert 4";
Desert[4]= "Desert 5";
alert(Desert[whichDesert]);
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM name="form1">
<center>
<INPUT TYPE="button" value="Click Here For Menu!" onClick="getaStarter();getaMeal();getaDesert()" button style="font: bold 14px Arial; color: black">
</center>
</FORM>
</BODY>
One Difference
There is one difference in the code which does need explaining but you'll probably have figured it out anyway on your own.
The line alert(Desert[whichDesert]) gets the writing which relates to the Desert selected at random and produces a small alert box which tells the user which desert the function has chosen.
What should happen
When the user clicks the button a attention box will pop up with a starter suggestion. Next a meal suggestion wiil pop up and finally a desert suggestion will appear.
PrintShare it! — Rate it: up down flag this hub










Ktoo says:
9 months ago
Thank you :) I'll give that a try and let you know how it worked for me!