I was tasked by my professor to make a simple Javascript program that displays simple math problems and their answers in a two dimensional table using a random number. Previously he gave us the example of using a function to write into a table like this:
function RandomGen() {
Random = math.floor(Math.random()*60);
document.writeln("<th>");
document.writeln(Random);
document.writeln("</th>");
}
RandomGen();
In order to use an array, can I do this?
var RandomArray [
RandomGen(),
second_function(),
third_function(),
forth_function(),
]
RandomArray[0];
How do I append the functions together to write into a table?
slebetman :
You almost got it. The correct syntax is:\n\nvar randomArray = [ \n RandomGen,\n second_function,\n third_function,\n forth_function\n];\n\nrandomArray[0](); // this calls RandomGen\nrandomArray[1](); // this calls second_function\n\n\nRemember the basic syntax rules:\n\n\nA function name with no paranthesis is a function reference. It behaves just like any reference and so can be treated as a variable. Which means you can assign it to another variable, pass it into another function and as the example above stuff it into an array.\nAdding paranthesis () to a function reference causes the function to be called. It doesn't matter if the function reference is a plain old function name or stored in another variable or stored in an array.\n",
2013-03-26T06:19:44