For a full description of all JavaScript operators please refer to W3schools JS
Add
Syntax:
num + num
Example:
speed = 0
console.log(speed)
control.wait(2)
speed = speed + 10
console.log(speed)
Subtract
Syntax:
num - num
Example:
speed = 100
console.log(speed)
control.wait(2)
speed = speed - 10
console.log(speed)
Multiply
Syntax:
num * num
Example:
speed = 10
console.log(speed)
control.wait(2)
speed = speed * 2
console.log(speed)
Divide
Syntax:
num / num
Example:
speed = 100
console.log(speed)
control.wait(2)
speed = speed / 2
console.log(speed)
Pick Random
Syntax:
Math.floor(Math.random() * (max - min)) + min
Example:
control.forever(function(){
speed = Math.floor(Math.random() * (100 - 20)) + 20
console.log(speed)
control.wait(1)
})
Greater Than
Syntax:
num > num
Example:
control.forever(function(){
if (eBot.sensors.line.analog.LHS > 50) {
console.log('I see white')
}
})
Less Than
Syntax:
num < num
Example:
control.forever(function(){
if (eBot.sensors.line.analog.LHS < 50) {
console.log('I see black')
}
})
Equal To
Syntax:
num/string/boolean === num/string/boolean
note: same type and value
Example:
control.forever(function(){
if (eBot.sensors.line.digital.LHSBlack === true) {
console.log('I see black')
}
})
AND
Syntax:
var && var
Example:
control.forever(function(){
if (eBot.sensors.line.digital.LHSBlack === true && eBot.sensors.ultra.analog.LHS < 50) {
console.log('I see black line and an object')
}
})
OR
Syntax:
expression || expression
Example:
control.forever(function(){
if (eBot.sensors.line.digital.LHSBlack === true || eBot.sensors.ultra.analog.LHS < 50) {
console.log('I either see black line or an object')
}
})
NOT
Syntax:
(!(condition))
Example:
control.forever(function(){
if (!(eBot.sensors.ultra.analog.LHS < 50)) {
console.log('ultra sensor is not less than 50cm')
}
})
Round
Syntax:
Math.round (variableName)
Example:
speed = 100
speed = speed /3
console.log(speed)
control.wait(1)
console.log('now round this to nearest integer')
speed = Math.round (speed)
console.log(speed)