Syntax Help With Glowscript (in JavaScript, not VPython)

247 views
Skip to first unread message

Jees

unread,
Jun 17, 2021, 4:46:46 PM6/17/21
to Glowscript Users
Hello all,

I was wondering if there happens to be a comprehensive guide on proper JavaScript syntax for GlowScript. My prior experience in programming mostly comes from taking AP Computer Science Principles, which I believe uses JavaScript. I am currently trying to create a set of three points with a randomly generated x/y coordinate pair, and so far I have come up with this:

let numListX = [ ];
for (let i = 0; i < 3; i++) {
   let NumX = randomNumber(0, 320);
  appendItem(numListX, NumX);
}

let numListY = [ ];
for (let i = 0; i < 3; i++) {
   let NumY = randomNumber(0, 450);
  appendItem(numListY, NumY);
}

for (let i = 0; i < 3; i++) {
points(pos:[vector(numListX,numListY[i],0)], radius=50,  color:color.red)
}

I have mostly created this in Code.org and copied/pasted it into GlowScript with a few tweaks. When it is run, I receive an unexpected token error. How should I best approach solving this issue?

Thanks for your time and help, and take care!

Bruce Sherwood

unread,
Jun 17, 2021, 6:16:54 PM6/17/21
to Glowscript Users
Almost all GlowScript users write their programs in Python, not JavaScript. If you need to write in JavaScript the first line of the program needs to be either "GlowScript 3.1 JavaScript" or "GlowScript 3.1" (at the start of the GlowScript project programs has to be written in JavaScript, so it's the default on that first line). If this isn't the answer to your question, please post a link to your program or at least report exactly what the error message is.

Also, if you do use JavaScript, you need to study this information:


Hmm. I don't recognize the function randomNumber; it doesn't seem to be a JavaScript function.

Bruce

Bruce Sherwood

unread,
Jun 17, 2021, 6:37:08 PM6/17/21
to Glowscript Users
Ah. I discovered that randomNumber() is a function provided in the Code.org environment. It is not a native JavaScript function.

I don't know anything about Code.org, so I don't know whether you used JavaScript or some other language in your studies there.

Bruce

Bruce Sherwood

unread,
Jun 17, 2021, 6:45:24 PM6/17/21
to Glowscript Users
JavaScript does have a function Math.random() which returns a floating-point number between 0 and 1. GlowScript imports JavaScript's Math library, so in a GlowScript JavaScript program you can just say random(). Here is a way to achieve the effect of randomNumber() in JavaScript:

for (let i=1; i<=10; i++) {
    let x = floor(10*random())
    print(x) # prints integers in the range of 0 to 9 inclusive
}

Bruce

Jees

unread,
Jun 17, 2021, 10:23:46 PM6/17/21
to Glowscript Users
Thanks for noticing that discrepancy from your second message and pointing out the helpful document; I'll also make sure to checkout JavaScript's Math library to ensure GlowScript understands the syntax. I appreciate the help!
Reply all
Reply to author
Forward
0 new messages