How to beat level 7

untrusted

In Untrusted, I got to Level 7, where I can get the phone, and create my own JS function. I saw from the demo that I can change the colour! Great! So I figured I'd write a nested if... then statement, saying if I'm one colour, switch to the next! Apparently, that does not work.

I then tried to change my colour to white (#fff), thinking the collision check would accept white as all colours, and open the locks. What can I put in this function to change me as needed?

enter image description here

Best Answer

You can get past all the gates using the code below:

if(player.getColor()=='#f00')
   player.setColor('#ff0');
else if(player.getColor()=='#ff0')
   player.setColor('#0f0');
else
   player.setColor('#f00');

Basically, it makes it so every time you hit Q, you change to the color of the next gate.
Green -> Red -> Yellow -> Green

Finish