Note that there are some explanatory texts on larger screens.

plurals
  1. PO"this" function with parameter
    primarykey
    data
    text
    <p>I've got a class Robot as defined below. But for some reason there's an error with the processCommand function and I can't figure out what it is. It's the only function that takes a parameter so I'm wondering if there's something wrong with the way I pass it in the readCommands function?</p> <p>Any help or opinion is appreciated!</p> <pre><code>function Robot(pos){ this.pos = pos; this.language; this.printCoords = function(){} this.setLanguage = function() {} this.setPos = function() {} this.processCommand = function(s){ var direction = this.pos[2]; direction = direction.replace(" ", ""); if( (s.match('V') &amp;&amp; this.language.match("svenska")) || (s.match('L') &amp;&amp; this.language.match("engelska")) ){ switch(direction){ case "N": // Look West this.pos[2] = " W"; break; case "E": // Look North this.pos[2] = " N"; break; case "S": // Look East this.pos[2] = " E"; break; case "W": // Look South this.pos[2] = " S"; break; default: break; } } else if( (s.match('H') &amp;&amp; this.language.match("svenska")) || (s.match('R') &amp;&amp; this.language.match("engelska")) ){ switch(direction){ case "N": // Look East this.pos[2] = " E"; break; case "E": // Look South this.pos[2] = " S"; break; case "S": // Look West this.pos[2] = " W"; break; case "W": // Look North this.pos[2] = " N"; break; default: break; } } else if( (s.match('G') &amp;&amp; this.language.match("svenska")) || (s.match('F') &amp;&amp; this.language.match("engelska"))){ switch(direction){ case "N": // Walk North this.pos[1] += 1; break; case "E": // Walk East this.pos[0] += 1; break; case "S": // Walk South this.pos[1] -= 1; break; case "W": // Walk West this.pos[0] -= 1; break; default: break; } } this.printCoords(); } this.readCommands = function() { document.getElementById("textField").onkeyup = function(key){ key = window.event; keyCode = key.keyCode || key.which; if(keyCode == '13'){ var commandString = document.getElementById("textField").value; for(var i = 0; i &lt; commandString.length; i++){ this.processCommand(commandString[i]); } } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload