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

  • @ヴァイシャリ I realize it now, but I couldn't spoil it all in the question! – Canadian Luke Apr 10 '14 at 18:55

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

else if(player.getColor()=='#ff0')

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


| improve this answer | |
  • Aaarg! I was using a single equals sign! I'm posting my (much longer) answer as well, but that would definitely work! – Canadian Luke Apr 8 '14 at 19:44
  • @CanadianLuke I do that all the time too. Now I'll have to play this game the rest of the day. – Batophobia Apr 8 '14 at 19:47
  • I like the old-style games, like Candybox 1 and 2, as well – Canadian Luke Apr 8 '14 at 19:49
  • Exactly what I did xD – Enoch Apr 8 '14 at 20:51

If you have solved it once, now you can play with it, improving the code. Thinking what else you can do, for example you can do something ugly and funny:

player.setPhoneCallback(function () {
  var i = 66;
  var callback = function(){
    player.setColor("#" + ((--i % 3) > 0 ? 'f' : '0') + ((i + 1) % 3 > 0 ? 'f' : '0') + '0')
  return callback;
(function doNothingHack(){ 

Also we can see that f00, ff0 and 0f0 can be represented as 100, 110, and 010, and this, as you can see are just 3 small integers (2, 3, 1) represented as binary. So modulo arithmetic, decimal to binary conversion then replace 1 with f, and we have another such solution.

| improve this answer | |

I decided to go ahead and post a few solutions of my own.


This one makes use of switch statements and saves each color as an object literal to improve readability and increase coding speed and efficiency:

var color = { 'green' : '#0f0',
              'red'   : '#f00',
              'yellow': '#ff0'};

switch(player.getColor()) {
    case color.green:
        player.setColor(color.red); break;

    case color.red:
        player.setColor(color.yellow); break;

    case color.yellow:
        player.setColor(color.green); break;


The default case at the end isn't necessary for that particular level, as the player's color is already green by default, but I decided to include it anyways for completion's sake.

Increment and modulo

This solution makes use of the increment operator ++ and the modulo operator %, making the code much, much shorter.

var colors = ['#0f0', '#f00', '#ff0'];
var i = 0;

player.setPhoneCallback(function() {
    player.setColor(colors[++i % 3]);


Since we can't add code outside the player.setPhoneCallback() method, it has become a bit hacky.

The idea behind this solution, is to define a color array which we will cycle through. For that, the increment and modulo operators are instrumental. For those who don't know: the modulo operator returns the rest of a division.

Since we don't want our variable i to be reset every time we use the phone, we have to redefine the phone method. Once we're done, we call player.setColor() to change our color. That part isn't necessary, but without it, we'd have to use the phone twice to change our color for the first time.

Once you understand the above code, we can shorten it even more:

if (!player.i) player.i = 0;
player.setColor(['#0f0', '#f00', '#ff0'][++player.i % 3]);
| improve this answer | |
  • I wanted to post a solution using setInterval, but apparently the method is not allowed. Damn it... – Nolonar Apr 12 '14 at 22:00

Found the solution! I inserted the following code:

if (player.atLocation(24, 12)) {
if (player.atLocation(27, 12)) {
if (player.atLocation(30, 12)) {
if (player.atLocation(33, 12)) {
if (player.atLocation(35, 12)) {

If I am at the x, y coordinates, then it will execute that one colour change for me.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.