Tuesday, June 18, 2013

JavaScript stack-walking loop!

The o in alert(isInLetterStack("o")); below may be replaced with any lowercase letter from a to j in the name of having an alert when comes back with true. Otherwise the alert will come back with false.

<html>
   <head>
      <script type="text/javascript">
         var letterStack = {
            "b":"a",
            "c":"b",
            "d":"c",
            "e":"d",
            "f":"e",
            "g":"f",
            "h":"g",
            "i":"h",
            "j":"i",
            "k":"j"
         }
         alert(isInLetterStack("o"));
         function isInLetterStack(target) {
            return inLetterStack("k");
            function inLetterStack(currentTarget) {
               if(!letterStack[currentTarget]) {
                  return false;
               }
               if(letterStack[currentTarget] == target) {
                  return true;
               }
               return inLetterStack(letterStack[currentTarget])
            }
         }
      </script>
   </head>
   <body />
</html>

No comments:

Post a Comment