0

I am making a MEL expression, however I believe the language is somewhat irrelevant. This question is more of a coding logic question. Normally I would do something like this in javascript (which I am more familiar with) and then translate it but I am kind of stuck with what method to use, i.e. if else, loops, switch, etc?

Here is an example of what I am trying to achieve(I made it in sort of a generic syntax so language doesnt matter so much):

Variable myA is a user input number variable.

Variable myB is a number variable that will adjust based on input from var myA in the following way.

myA:Number;
myB:Number;

my_bool = true; 
if (!my_bool){
    myB*=-1;
}

if(myA>45){
    my_bool = false;
    //for every 90 units up switch boolean
}

Basically the comment part is where I am stuck. I want to setup a switch that functions after myA is greater than 45, and then from that point on, switch the my_bool when the var myA goes up 90 units. 45+90=135, so at 135 it will switch to true and 90 units later when myA equals 225, my_bool will switch to false and so on indefinitely. I dont know if I should use an if else, some kind of loop, or if there is something that fits this situation perfectly.

  • 1
    Generally speaking, just a flag that changes like this `if (myB % 45) flag = !flag` etc. is the way to go – adeneo Jan 01 '16 at 17:54

3 Answers3

0

This should solve your problem :

function my_bool(myA){
  var
    doNothing = myA <= 44,
    step = parseInt((myA - 45)/90)%2,
    myA = step == 1;

  return doNothing ? "nothing" : myA;
}

it returns "do nothing", true, false.

Nishant
  • 4,659
  • 2
  • 27
  • 43
  • Looks interesting but could you explain what is happening. A few things here I dont recognize. Where does step come from? I've used parseInt before but only to extract numbers from strings, I never seen [] inside of it, couldnt it just be `parseInt ((myA - 45)/90%2)`? If not, why? I did a JS test of `console.log(doNothing);` and I get true when myA is 45 or less and false when it is above 45. However when it hits 135 it doesnt revert back to true. I want it to alternate: 0-45 is true, 46-135 is false, 136-225 is true, 226 to 360 is false. Thanks! – macprodukshunz Jan 01 '16 at 22:30
0

I've tried the following algorithm to fulfill your needs I'll explain them after the code:

my_a = input()
my_b = 0

my_bool = True

if my_a > 45:
    for num in range(0,400):
        if (num + 45) % 90 == 0 and not num==45:
            if my_bool == True:
                my_bool = False
            else:
                my_bool = True
            print 'My bool as switched to %s ' % my_bool
        else:
    print num
else:
   my_b -= 1 

I've used python here and what this does is: If the user input on my_a variable is greater than 45, then it checks within a range from 0 to 400 if the number (plus the first 45) is divisible by 90 and not 45 itself (because we don't want to toggle the boolean at 45, right?). If it's divisible, then checks if the boolean is actually True or False, and based in that it toggles it. And prints the result.

If none of the above happens then it just prints the current number in the for loop.

And if my_a input was less than 45 then my_b gets a -1 value.

Hope it helps!

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
Luiti E.
  • 1
  • 2
-1

Something along the lines of

while True:
        if ((myA-45)/90).is_integer():
            my_bool = not my_bool
        myA += 1
ysrome
  • 71
  • 1
  • 9