Is the line from input and line at runtime not equal?

So, I'm trying to read the input <textarea> , dividing it into separate words, and then check each word if it is equal to the word in the list of keywords.

I finished the code, and when I tested it, it did not work. When I debugged it, I saw that when it compares a string from a <textarea> string and a constant that looks the same, it considers them unequal.

I tried to take a string from <textarea> (in the same way, copying it) and compared it to a constant string statically, and then it became true.

If you know what the problem is, I will be glad to hear. Thanks.

Code:

 function run() { debugger; code = document.getElementById("codeArea").value; cleanDots(); words = code.split(" "); compile(); } function compile() { for (i = 0; i < words.length; i++) { var w = words[i]; if (w == "test") alert("test - true"); } } function cleanDots() { for (var j = 0; j < code.length; j++) { if (code.charAt(j) == ".") { var p1 = code.substring(0, j); var p2 = code.substring(j + 1, code.length); code = p1 + " " + p2; } } } 

HTML:

 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>דוס סקריפט סביבת פיתוח</title> <style type="text/css"> body{ padding: 0; margin: 0; } textarea{ width: 95%; padding: 10px; height: 700px; text-align: right; float: right; } button{ width: 95%; padding: 10px; text-align: center; float: right; background-color: green; } </style> </head> <body> <textarea id="codeArea"> </textarea> <button id="submit" onclick="run()">הרץ</button> <script src="compiler.js"></script> <script src="methods.js"></script> </body> </html> 
+5
source share
3 answers

First of all, what you actually do in the cleanDots function implements the JavaScript replace() method, this function should look something like this:

 function cleanDots() { code = code.replace(".", " "); } 

 function run() { debugger; code = document.getElementById("codeArea").value; cleanDots(); words = code.split(" "); compile(); } function compile() { for (i = 0; i < words.length; i++) { var w = words[i]; if (w == "test") alert("test - true"); } } 
 body { padding: 0; margin: 0; } textarea { width: 95%; padding: 10px; height: 700px; text-align: right; float: right; } button { width: 95%; padding: 10px; text-align: center; float: right; background-color: green; } 
 <textarea id="codeArea"> </textarea> <button id="submit" onclick="run()">הרץ</button> <script src="compiler.js"></script> <script src="methods.js"></script> 

Other than that, your code will execute accurately.

+1
source

In your cleanDots function, you want to replace all points with spaces, you can simply do:

 function cleanDots() { code = code..replace(/[\.]/g, " "); } 

with the regular expression /[..BIZ/g, you try to do all the occurrences of a point, and then replace it with space.

0
source

I really don't know what the problem was. I rewrote the code, reopened it in the browser, and it worked. I could not find the difference between the published code and the new one.

The only differences I found varied from if switch , but I checked, and now it works with if . another thing that I changed is to replace my < cleanDots method with code.replace(".", " ") , as suggested by chsdk. I also checked here and it did a great job with my method.

New code:

 function run() { debugger; code = document.getElementById("codeArea").value; code = code.replace(".", " "); words = code.split(" "); compile(); } function compile() { for (i = 0; i < words.length; i++) { var w = words[i]; switch(w) { case "הדפס": dosPrint(); break; } } } 

Thank you anyway.

0
source

All Articles