Rudy the Red Dot
JS is so meh to write in, why learn it here.  Instead, learn DRUL, the assembler made for solving Rudy The Red Dot:<p>code = compile(&quot;.:dddddrruuuul@.&quot;);<p>var colors = [];<p>var acc = 0;<p>function run(instructions) {  
   var pc = 0;<p><pre><code>   while (pc &lt; instructions.length) {
     var line = instructions[pc];
   var op = line[0];
     var args = line[1];
     switch (op) {
       case &quot;a&quot;: &#x2F;&#x2F; set accumulator (set to remainingDots() if no arg)
  a(args[0]);
  break;
       case &quot;+&quot;: &#x2F;&#x2F; add to accumulator (+1 if no arg)
  add(args[0]);
  break;
       case &quot;-&quot;: &#x2F;&#x2F; subtract from accumulator (-1 if no arg)
  sub(args[0]);
  break;
       case &quot;u&quot;: &#x2F;&#x2F; up n squares (1 if no arg)
  u(args[0]);
  break;
       case &quot;d&quot;: &#x2F;&#x2F; down n squares (1 if no arg)
  d(args[0]);
  break;
       case &quot;l&quot;: &#x2F;&#x2F; left n squares (1 if no arg)
  l(args[0]);
  break;
       case &quot;r&quot;: &#x2F;&#x2F; right n squares (1 if no arg)
  r(args[0]);
  break;
       case &quot;g&quot;: &#x2F;&#x2F; get current square color: color index n, unshift if -1, or push if no arg
  g(args[0]);
  break;
       case &quot;s&quot;: &#x2F;&#x2F; set current square color: color index n, pop if -1, or shift if no arg
  s(args[0]);
  break;
       case &quot;=&quot;: &#x2F;&#x2F; if current square color is literal (b &#x2F; r &#x2F; g &#x2F; y &#x2F; w) or color index n then jump to label
  if (eq(args[0])) {
   pc = args[1];
   continue;
  }
  break;
       case &quot;!&quot;: &#x2F;&#x2F; if current square color is not literal (b &#x2F; r &#x2F; g &#x2F; y &#x2F; w) or color index n then jump to label
  if (!eq(args[0])) {
   pc = args[1];
   continue;
  }
  break;
       case &quot;@&quot;: &#x2F;&#x2F; jump to label
  pc = args[0];
  continue;
       case &quot;0&quot;: &#x2F;&#x2F; if accumulator is 0 then jump to label
  if (acc == 0) {
    pc = args[0]
    continue;
  }
  break;
       case &quot;~&quot;: &#x2F;&#x2F; if accumulator is not 0 then jump to label
  if (acc != 0) {
    pc = args[0]
    continue;
  }
  break;
        }
  pc++;
   }
 }


 run(code);


 function compile(code) {
  var instructions = [];
   var waiting_for_op = true;
   var max_args = 0;
   var skip_if_non_number = false;
   var op = &quot;&quot;;
   var label = &quot;&quot;;
   code += &quot;     &quot;;

   var i = 0;
   var line = [];
   var args = [];

   while (i &lt; code.length) {
     var c = code[i];
     if (!waiting_for_op &amp;&amp; skip_if_non_number &amp;&amp; c != &quot;-&quot; &amp;&amp; isNaN(parseInt(c))) {
  line.push(args);
  instructions.push(line);
  line = [];
  waiting_for_op = true;        
     }     
     if (waiting_for_op) {
       if (c == &quot; &quot;) {
  i++
  continue;
       }
       if (code[i+1] == &quot;:&quot;) {
  label = c;
  i += 2;        
  op = code[i];
       }
       else {
  label = &quot;&quot;;
     op = c;
       }
       waiting_for_op = false;
       line = [label, op];
       args = [];
       skip_if_non_number = (op == &quot;u&quot; || op == &quot;d&quot; || op == &quot;l&quot; || op == &quot;r&quot;);
       if (op == &quot;=&quot; || op == &quot;!&quot;)
  max_args = 2;
       else
  max_args = 1;      
     }
     else {
       if (c == &quot; &quot;) {
  line.push(args);
  instructions.push(line);
  line = [];
  waiting_for_op = true;        
       }        
    else {
  var negate = false;
  if (c == &quot;-&quot;) {
    negate = true;
    i += 1;
    c = code[i];
  }
  var num = parseInt(c);
  if (isNaN(num)) {
    if (c == &quot;b&quot;)
      args.push(&quot;blue&quot;);
    else if (c == &quot;r&quot;)
      args.push(&quot;red&quot;);
    else if (c == &quot;y&quot;)
      args.push(&quot;yellow&quot;);
    else if (c == &quot;g&quot;)
      args.push(&quot;green&quot;);
    else if (c == &quot;w&quot;)
      args.push(&quot;white&quot;);
    else
      args.push(c);          
  } 
  else {          
    c = &quot;&quot;;
    while (!isNaN(num)) {
      c += code[i];
      i++;
      num = parseInt(code[i]);
    }
    num = parseInt(c);
    if (negate) num = -num;      
    i--;
    args.push(num);
  }
  if (max_args &gt; 0 &amp;&amp; args.length &gt;= max_args) {
    line.push(args);
    instructions.push(line);
    line = [];
    waiting_for_op = true;       
  }
       }
     }

     i++;
   }

   for (i = 0; i &lt; instructions.length; i++) {
     line = instructions[i];
     op = line[1];
     if (op == &quot;=&quot; || op == &quot;!&quot;)
       instructions[i][2][1] = get_label(instructions, instructions[i][2][1]);
     else if (op == &quot;@&quot; || op == &quot;0&quot; || op == &quot;~&quot;)
       instructions[i][2][0] = get_label(instructions, instructions[i][2][0]);

   }

   for (i = 0; i &lt; instructions.length; i++) 
  instructions[i].shift();

   return instructions;
 }



 function get_label(lines, label) {
   for (var i = 0; i &lt; lines.length; i++) {
     if (lines[i][0] == label)
       return i;
   }
   return -1;
 }


 function repeat(fn, n) {
   if (n == undefined)
     fn();
   else
  for (var i = 0; i &lt; n; i++)
  fn();
 }

 function eq(color) {
   if (typeof(color) == &quot;number&quot;)
     color = colors[color];
   if (color == &quot;white&quot;)
     color = false;
   return color == getColor();
 }

 function g(c) {
  color = getColor();
   if (!color) 
     color = &quot;white&quot;;

   if (c == undefined)
     colors.push(getColor());
   else if (c == -1)
     colors.unshift(getColor());
   else
  colors[c] = getColor();
 }

 function s(c) {
   if (c == undefined)
     color = colors.shift();
   else if (c == -1)
     color = colors.pop();
   else
     color = colors[c];

   if (color == &quot;white&quot;)
     color = false;
   setColor(color);
 }

 function u(n) {
   repeat(up, n);
 }

 function d(n) {
   repeat(down, n);
 }

 function r(n) {
   repeat(right, n);
 }

 function l(n) {
   repeat(left, n);
 }

 function a(n) {
   if (n == undefined)
     acc = remainingDots();
   else
     acc = n;
 }

 function add(n) {
   if (n == undefined)
     acc++;
   else
  acc += n;
 }

 function sub(n) {
   if (n == undefined)
     acc--;
   else
     acc -= n;
 }

 &#x2F;&#x2F; level  1  d7r5u2r2
 &#x2F;&#x2F; level  2  d6r3u2r2d3
 &#x2F;&#x2F; level  3  d2 g d3r2 s r2u
 &#x2F;&#x2F; level  4  d4r1g ds r2urg ds r2
 &#x2F;&#x2F; level  5  .:r2 =bB =rR r B:d @. R:u @.
 &#x2F;&#x2F; level  6  r =bB u4 =b&gt; lu &gt;:ru B:d4 =b&lt; rd &lt;:ld
 &#x2F;&#x2F; level  7  .:=r&gt; d @. &gt;:r2 u9 @.
 &#x2F;&#x2F; level  8  d8 r2 u7 r2 d7 r2 u7 r2 d7 r1
 &#x2F;&#x2F; level  9  .:r100 d100 @.
 &#x2F;&#x2F; level 10  a8 .:d g - ~. dr a8 #:s ru - ~# u
 &#x2F;&#x2F; level 11  .:=gG=yY=bB=rR G:r=wG@. Y:d=wY@. B:u=wB@. R:l=wR@.</code></pre>