4

In Geogebra let's define a JavaScript global function from a button (property):

function ggbOnInit() {
}

function test(par) {
  return par * math.random();
}

How do I call that function from a Geogebra script attached to that button ("on click" property)?

The following Geogebra script:

Sequence[test(3),k,1,5]

launches an error message " unknown command test", whereas a sequence of five random numbers between 0 and 3 were waited.

soegaard
  • 30,661
  • 4
  • 57
  • 106
Jean-Pat
  • 1,839
  • 4
  • 24
  • 41

3 Answers3

3

I found this possible solution.

Create a button and in the "On Click" section, with "JavaScript" selected at the bottom, insert the code:

ggbApplet.evalCommand("list={0}");
if (! ggbApplet.exists("par")) {ggbApplet.evalCommand("par=200")}; 
for(var i =1;i<5;i++) 
ggbApplet.evalCommand("SetValue[list," +i+",  par* random()]");

The par parameter can be set as a Geogebra variable or inside the code (that last value will be used in case the variable doesn't exist).

Anyway I couldn't find how to use a function in the Global Javascript tab or how to use the Javascript math.random() command.

The problem is that the JavaScript commands available inside Geogebra are probably a limited set of the JavaScript available outside it, but I'm not sure about that.

Luca M
  • 31
  • 1
2

In order to call your Global JavaScript function your "On click" script must be of type JavaScript and not GeoGebra Script.

  • Try to provide more explanation to your answers, like a possible code example. See [How to answer page](http://stackoverflow.com/help/how-to-answer) for help in improving your answer. – Madness Aug 22 '15 at 23:21
-3

I'm not sure whether you want to get five random integers or decimal numbers. If you want to get five random integers between 0 and 3 you can easily do it with GeoGebra scripting. You can first create an empty list writing for example list={} into Input Bar and then fill it with a button or create it dynamically with a button.

Create a button, open Object Properties, go to Scripting tab and inside On Click tab write next line: list=Sequence[RandomBetween[0,3],k,1,5]

Be sure that you've chosen "GeoGebra Script" in drop-down menu.

IF YOU WANT FIVE RANDOM DECIMAL NUMBERS between 0 and 3 do the same but with little different code line: list1=Sequence[random()+RandomBetween[0,2],k,1,5]

Hope this helped...

Alex
  • 1
  • 1