Is this acceptable in a JavaScript if
statement?
(value === 0 && option_1 === 1 && option_2 === 1)
If yes, what could be the reason why my script doesn't work? (ie. why nothing is being sent when the condition is met)
Full script
var num = getProp('this', 'id').replace(/[^0-9]/g,'');
var option_1 = get("option_1")
var option_2 = get("option_2")
let myString1 = parseFloat(num)-1;
if (value === 1 && option_1 === 0) {
send('/note', 6, myString1, 127)
}
else if (value === 1 && option_1 === 1) {
send('/note', 6, myString1, 127)
}
else if (value === 0 && option_1 === 1) {
setTimeout(function(){
send('midi:SessionKiano', '/note', 2, 85, 127)
}, 100)
}
// doesn't work
else if (value === 0 && option_1 === 1 && option_2 === 1) {
console.log("Hello!")
send('midi:SessionKiano', '/note', 2, 86, 127)
}
// doesn't work
else if (value === 0 && option_1 === 1 && option_2 === 2) {
send('midi:SessionKiano', '/note', 2, 87, 127)
}
// doesn't work
else if (value === 0 && option_1 === 1 && option_2 === 3) {
send('midi:SessionKiano', '/note', 2, 88, 127)
}
To answer my own post, yes, it's acceptable.
I rewrote the script and now it looks like this:
var num = getProp('this', 'id').replace(/[^0-9]/g,'');
var option_1 = get("option_1")
var option_2 = get("option_2")
let myString1 = parseFloat(num)-1;
/////////////////////////////////////////////////////////////////////////////////
// when the button is switched on
if (value === 1) {
send('/note', 6, myString1, 127) // open key editor
}
/////////////////////////////////////////////////////////////////////////////////
// when the button is switched off
else if (value === 0 && option_1 == 1 && option_2 == 1) {
send('midi:SessionKiano', '/note', 2, 86, 127) // controller lane configuration 1
setTimeout(function(){
send('midi:SessionKiano', '/note', 2, 85, 127) // activate next part
}, 200)
}
else if (value === 0 && option_1 == 1 && option_2 == 2) {
send('midi:SessionKiano', '/note', 2, 87, 127) // controller lane configuration 2
setTimeout(function(){
send('midi:SessionKiano', '/note', 2, 85, 127) // activate next part
}, 200)
}
else if (value === 0 && option_1 == 1 && option_2 == 3) {
send('midi:SessionKiano', '/note', 2, 88, 127) // controller lane configuration 3
setTimeout(function(){
send('midi:SessionKiano', '/note', 2, 85, 127) // activate next part
}, 200)
}
l01c
April 25, 2021, 8:28am
3
I like to put many parenthesis in my complex if statements to make sure it is computed the way I want it:
If ((a==1) && (b==1) && (c==1)) {...
Why do you think it won't be computed right without parenthesis?
Parenthesis are not needed in this case (but harmless). Some reading on the subject:
javascript - When should you use parentheses inside an if statement's condition? - Stack Overflow
Operator precedence - JavaScript | MDN
TLDR; parenthesis are useful if you're mixing AND (&&) and OR (||) conditionals, or if the comparison expression is complex and makes it difficult to understand in which order things will be resolved.
1 Like