assurance-tunnel
assurance-tunnel
assurance-tunnel
assurance-tunnel

Javascript "or" operator [solved]



  • Hello,


    I want to do a javascript actor which has a string input (the name of a sequence) and return an integer depending on the input. As many sequences name will output the same number, i wanted to use the "or" operator to keep my code as short as possible like:

    function main()
    {
        var step = arguments[0]
        if(step == '3'|| '4'|| '5'|| '6A'|| '6B'|| '6C'|| '7A'|| '7B'|| '8A'|| '8B'){
            return 1;    
        }else if(step == '11B'|| '11C'){
            return 2;
        }else{
            return 100;
        }
    }
     

    But it works only for the first test of the first statement (here "3" ) and then is unable to test anything else, even in the next if statement...

    Is there some mistake in my code or is it a Isadora limitation?

    I've got it working around by using a if statement for each possibility, so it's more a cosmetic problem and general knowledge... :)

    Anybody knows about it? (made in Isadora 2.6.1)


  • Beta Platinum

    Personally I would write something like this :

    function main()
    {
        var step = arguments[0];
        
        switch(step) {
            
            case "3" || "4" || "5" | "6A" || "6B" || "6C" || "7A" || "7B" || "8A" || "8B":
                return 1;
            case "11B" || "11C":
                return 2;
            default:
                return 100;
        }
    }


    This is called a Switch statement in JavaScript and meant for complex cases where we have to do multiple long if statements. It will work it's way through the stack, until it gets a break (or a return in this case, since a return also breaks the loop)


  • Beta Platinum

    Regarding your OP question :

    Take this line of code :

    step = "3";
    if (step == "1" || "2"){

    return "This should not execute !";

    }

    else {

    return "Yeah !";

    }

    The above code snippet wil return "This should not execute !", you expect that the code will be skipped since it is false right ? That is not the case ! step == "1" will return false, since our string is "3", but "2" will return true. Why is this ? Because we aren't saying is step == "2", we are saying is "2" equal to true ? And the answer in JavaScript to that question is yes (True)


    If True / False are combined it will return the last positive value. So in this case the code will stop by "2", causing the return "This should not execute !" to be executed.



  • thanks @Juriaan for the detailed answer.

    I didn't think of using a switch, but it's true it's much smoother than what I did.

    Nonetheless, I still have the same problem than before, it will work with the first values of each case ( "3" and "11B" in the example code) but the following values will return the default value.

    So the "or" operator is not working as far as I understand

    (thanks for the 2nd explanation also, I realise my mistake now)


  • Tech Staff

    @maxime

    try breaking your logic up...
    eg: if (step == "1" || step == "2")

    see for more options: https://stackoverflow.com/ques... (ignore the jQuery stuff, and note EcmaScript 6 'mostly, as supported by Googles' V8 engine' is what is supported in Isadora 2.6.1)



  • thanks @DusX 

    I indeed did a mistake in my first attempt with if statement and then jump to Juriaan solution with the switch without spending much time on my first attempt.

    So the "or" operator works properly in the "if" statement when properly repeating the condition each time (well explained by Juriaan in his second comment)

    While the switch case like in @Juriaan first comment doesn't work because switch doesn't take "or" operator ( as explained here ) , instead, you must do something called a "fall-through":

    function main()
    {
    var step = arguments[0];
    switch(step){
        case 'seq1A':
        case 'seq1B':
        case 'seq1C':
            return 1;
        case 'seq2A':
        case 'seq2B':
            return 2;
        default:
            return 100;
    }
    }

    I've got it mixed up by trying several solutions in parallel without understanding the specificities of each one, but it's all working now. 

    Thanks for your help, I've learned something today! :)