Question

Found and fixed my own error. The problem was that I've had another variable called "map" in a different document, so with that being around my code didn't work. I'll just leave this code here as an example of how to do a map.

<html>
   <head>
   <script type="text/javascript">
      var map = {};
      map["red"] = "not avaliable";
      map["blue"] = "avaliable";

      function car(color){
         this.color = color;
      }
      function initialize(){
         var testCar = new car("blue");

         alert("Value is obviously blue: " + testCar.color);
         if (testCar.color in map) {
            var mappedValue = map[testCar.color];
                console.log("Your car in "+ testCar.color + " is "+ mappedValue);
         } else {
            console.log("No color "+ testCar.color + "in maps");
         }
      }
   </script>
   </head>
   <body onload="initialize()"></body>
</html>
Was it helpful?

Solution

Problem solved. Be careful you don't have variables with the same name even in different documents.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top