2

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?

2
  • Are you trying to store functions into an array and then run them from the array? Commented Mar 26, 2013 at 5:58
  • yes, and have the function write the html as well, at the professors request. Commented Mar 26, 2013 at 6:02

2 Answers 2

3

You almost got it. The correct syntax is:

var randomArray = [    
  RandomGen,
  second_function,
  third_function,
  forth_function
];

randomArray[0](); // this calls RandomGen
randomArray[1](); // this calls second_function

Remember the basic syntax rules:

  1. A 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.

  2. Adding 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.

Sign up to request clarification or add additional context in comments.

Comments

1

You can store the function in a variable, then put the variables in an array, and then access them from the array to call them.

var add = function(x,y){ return x + y; };
var multiply = function(x,y){ return x * y; };
var maths = [];
maths.push(add);
maths.push(multiply);
maths[0](1,2);//3
maths[1](3,4);//12

Conversely, you could do this with an object.

var maths = {};
maths.add = function(x,y){ return x + y; };
var four = maths.add(1,3);

Obviously you will have to modify these to match your exact situation.

5 Comments

gotcha, how would i then write into a table?
I keep pressing enter sorrylol document.writeln("<th>") maths[0](1,2); document.writeln("</th>")?
@user2210274 - If you wanted maths content in the document.write it would be document.writeln("<th>"+maths[0](1,3)+"</th>"); where the + sigh concatenates the strings together. That would result in writing <th>4</th> to the page.
do you know how you would then print an element inthe array to the DOM into a html element using getelementbyid?
@user2210274 - You would use the document.createElement and .appendChild methods. Here is a simple example: stackoverflow.com/a/15580994/1026459

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.