JS4Scratch Reference Manual:
For a full description of all JavaScript operators please refer to W3schools JS
Add
Syntax:
num + numExample:
spriteA.whenFlag(function () {
this.show()
score = 0
this.sayWait('The score at the moment is:',2)
this.sayWait(score,2)
this.sayWait('but if we add 2 it becomes:',2)
score = score + 2
this.sayWait(score,2)
})Subtract
Syntax:
num - numExample:
spriteA.whenFlag(function () {
this.show()
score = 10
this.sayWait('The score at the moment is:',2)
this.sayWait(score,2)
this.sayWait('but if we substract 2 it becomes:',2)
score = score - 2
this.sayWait(score,2)
})Multiply
Syntax:
num * numExample:
spriteA.whenFlag(function () {
this.show()
score = 10
this.sayWait('The score at the moment is:',2)
this.sayWait(score,2)
this.sayWait('but if we multiply by 2 it becomes:',2)
score = score * 2
this.sayWait(score,2)
})Divide
Syntax:
num / numExample:
spriteA.whenFlag(function () {
this.show()
score = 10
this.sayWait('The score at the moment is:',2)
this.sayWait(score,2)
this.sayWait('but if we divide by 2 it becomes:',2)
score = score / 2
this.sayWait(score,2)
})Pick Random
Syntax:
Math.floor(Math.random() * (max - min)) + minExample:
spriteA.whenFlag(function () {
this.show()
this.sayWait('I will display a random integer 1-10 every 1 second',2)
score = 0
while (true) {
score = Math.floor(Math.random() * (10 - 1)) + 1
this.sayWait(score,1)
}
})Greater Than
Syntax:
num > numExample:
spriteA.whenFlag(function () {
this.show()
score = 0
this.sayWait('Increment the score and determine if greater than 10',2)
while (true) {
score = score + 1
this.wait(0.5)
if (score > 10) {
this.say('score is now larger than 10')
}
}
})Less Than
Syntax:
num < numExample:
spriteA.whenFlag(function () {
this.show()
score = 10
this.sayWait('Decrement the score and determine if less than 0',2)
while (true) {
score = score - 1
this.wait(0.5)
if (score < 0) {
this.say('score is now less than 0')
}
}
})Equal To
Syntax:
num === numnote: same type and value
Example:
spriteA.whenFlag(function () {
this.show()
score = 10
this.sayWait('Decrement the score and determine if equal to 5',2)
while (true) {
score = score - 1
this.wait(1)
if (score === 5) {
this.sayWait('score is now equal to 5',2)
} else {
this.say('Score is not equal to 5')
}
}
})AND
Syntax:
var && varExample:
spriteA.whenFlag(function () {
this.show()
this.sayWait('Press the Up and Down keys when you are ready',2)
while (true) {
if (stage.isKeyPressed('ArrowUp') && stage.isKeyPressed('ArrowDown')) {
this.say('Both Up and Down arrow keys are pressed')
} else {
this.say('Both Up and Down arrow keys are NOT pressed')
}
}
})OR
Syntax:
var || varExample:
spriteA.whenFlag(function () {
this.show()
this.sayWait('Press either Up or Down keys when you are ready',2)
while (true) {
if (stage.isKeyPressed('ArrowUp') || stage.isKeyPressed('ArrowDown')) {
this.say('either Up or Down arrow key is pressed')
} else {
this.say('neither Up nor Down arrow key is pressed')
}
}
})NOT
Syntax:
!(condition)Example:
spriteA.whenFlag(function () {
this.show()
score = 10
this.sayWait('Decrement the score and determine if not equal to 5',2)
while (true) {
score = score - 1
this.wait(1)
if (!(score === 5)) {
this.sayWait('score is not equal to 5',2)
} else {
this.sayWait('Score is equal to 5',2)
}
}
})Round
Syntax:
Math.round (variableName)Example:
spriteA.whenFlag(function () {
this.show()
score = 5.8
this.sayWait('The score at the moment is:',2)
this.sayWait(score,2)
this.sayWait('but if we round that it becomes:',2)
score = Math.round (score)
this.sayWait(score,2)
})