For a full description of all JavaScript operators please refer to W3schools JS
Add
Syntax:
num + numExample:
speed = 0
console.log(speed)
control.wait(2)
speed = speed + 10
console.log(speed)Subtract
Syntax:
num - numExample:
speed = 100
console.log(speed)
control.wait(2)
speed = speed - 10
console.log(speed)Multiply
Syntax:
num * numExample:
speed = 10
console.log(speed)
control.wait(2)
speed = speed * 2
console.log(speed)Divide
Syntax:
num / numExample:
speed = 100
console.log(speed)
control.wait(2)
speed = speed / 2
console.log(speed)Pick Random
Syntax:
Math.floor(Math.random() * (max - min)) + minExample:
control.forever(function(){
speed = Math.floor(Math.random() * (100 - 20)) + 20
console.log(speed)
control.wait(1)
})Greater Than
Syntax:
num > numExample:
control.forever(function(){
if (eBot.sensors.line.analog.LHS > 50) {
console.log('I see white')
}
})Less Than
Syntax:
num < numExample:
control.forever(function(){
if (eBot.sensors.line.analog.LHS < 50) {
console.log('I see black')
}
})Equal To
Syntax:
num/string/boolean === num/string/booleannote: same type and value
Example:
control.forever(function(){
if (eBot.sensors.line.digital.LHSBlack === true) {
console.log('I see black')
}
})AND
Syntax:
var && varExample:
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 || expressionExample:
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)