Same code, same browser, different user behavior

I have a JS code snippet that parses a file and then associates an array with a map of key and value values, and then iterates through it to find the correct city name using the .includes method. My problem is that the last field (where the function is called) works fine at my end for both Chrome and Firefox. For some reason this does not work for members of my group.

This is a JS snippet that iterates:

Edit: this opens the file:

var rawFile = new XMLHttpRequest(); rawFile.open("GET", "../data/myFile.txt", false); 

 for (var i = 0; i < allText.length; i++) { if (i % 2 == 0) { myMap[allText[i]] = allText[i + 1]; } } var city = document.getElementById("city").value; for (var key in myMap) { if (myMap.hasOwnProperty(key)) { if (myMap[key].toLowerCase().includes(city.toLowerCase())) { document.getElementById("city").value = myMap[key] document.getElementById("zipcode").value = key; } } } 

This is the part of html that calls it:

 <label for="myLabel">City: </label> <input type="text" name="myLabel" id="myLabel" onblur="readTextFile()"> 

What exactly is the problem and how can I fix it, since it does not make any sense to me, coming from the world of Java and C ++, where I had never encountered such a problem before. If you're wondering why JS can be a little ugly, this is the result of a student with a teacher who believes that showing W3Schools examples is tantamount to good learning.

+8
javascript html
source share
1 answer

The Javascript function may be unstable due to the fact that some browsers do not support it. You need to be wise when choosing javascript features, especially when mozilla may have some features that are not supported in some browsers. W3schools also provides a browser support list for the feature. Check out the link below to enable this list:

http://www.w3schools.com/jsref/jsref_includes.asp

Alternatively, you can use the indexOf function, for example:

  myMap[key].toLowerCase().indexOf(city.toLowerCase()) >= 0 

I myself ran into problems, including a function, so I provide you with a workaround. Happy programming.

+1
source share

All Articles