Zig zag and zoom


#1
Hey guys. Can somebody explain me the logic of this code please.


function mod15(n) {
    while (n >= 15) {
        n -= 15;
    }
    return n;
}

// This function should return a value from 0 to 9:
function mod9(n) {
    // While n is greater or equal to 9, subtract 9 from n:
    while(n >= 9) {
        n -= 9; 
    }
    return n;
}

// Don't change the following code:
while (true) {
    var time = hero.time;
    var x, y;
    if (time < 30) {
        y = 10 + 3 * mod15(time);
    } else {
        y = 20 + 3 * mod9(time);
    }
    x = 10 + time;
    hero.moveXY(x, y);
}

#2

Maybe the explanations given here can help you:


Code samples are in Python, but the underlying logic is discussed and should be the same. The key for me was that the defined function is actually only a subtraction, but becomes a modulo calculation by including it in a while-True loop. How exactly this mathematical operation helps you evade the missiles is something you’ll have to ask the developers :wink:
If you have any more questions, you’ll need to be more specific though.
Also, don’t include your question in the code formatting.