Stack variables in JSXGraph for loop

Stack variables in JSXGraph for loop

ved Ralf Hoheisel -
Antal besvarelser: 2

Hi,

I'm trying to set up a stack question where I want to loop through arrays of koordinates "xo,yo" (which are going to be randomised later) to display points in JSXGraph,
but have trouble with the variables i'm using, specially with the increment variable "i".

What am I doing wrong here.

Thanks in advance for any help.

Best regards Ralf



Stack Variables:

k: 5;
xo: [1,2,3,4,5];
yo: [-2,-1,0,1,2];

Question Text:

jsxgraph

var board = JXG.JSXGraph.initBoard(divid, {axis: true, showCopyright: false, boundingbox: [-1,6,6,-1]});
function islessthan(a,b) {return Object.is((a-b)%1,-0);}
for (let i=0; islessthan(i,{#k#}); i++) {var P = board.create('point', [{#xo[i]#},{#yo[i]#}]);}                /*not working*/
for (let i=0; islessthan(i,{#k#}); i++) {var P = board.create('point', [{#xo[4]#},{#yo[4]#}]);}             /*works for certain number*/

/jsxgraph

Gennemsnitsbedømmelse: -
I svar til Ralf Hoheisel

Re: Stack variables in JSXGraph for loop

ved Bernhard Gailer -
Hello Ralf,

when scanning the question text, STACK replaces the variables within the braces ({##}) with the question variables. This is possible for only the Maxima question variables, however. In the code you posted, you try to mix the Maxima variable xo with the JavaScript loop index variable i. When scanning the code, STACK does not recognize the variable i since it is not in the question variables. If using an integer as index, this works, because the expression xo[4] can be evaluated in Maxima.

To solve this, you can assign the Maxima variables to JavaScript variables inside the JSXGraph block like this:

jsxgraph
var board = JXG.JSXGraph.initBoard(divid, {axis: true, showCopyright: false, boundingbox: [-1,6,6,-1]});
function islessthan(a,b) {return Object.is((a-b)%1,-0);}
// Point Coords
const xo = {#xo#};
const yo = {#yo#};
// Loop
for (let i=0; islessthan(i,{#k#}); i++) {var P = board.create('point', [xo[i],yo[i]]);
}                /*should now work*/
/jsxgraph

Best regards
Bernhard