Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String from input and string in runtime aren't equal?

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

I've finished the code, and when I tested, it didn't worked. When I debugged it, I saw that when it compares a string from the <textarea> and a constant string, which looks the same, it considers them as unequal.

I've tried to take the string from the <textarea> (exactly the same, copy pasted it) and compared it with the constant string statically, and then it became true.

If you know what the problem might be, I'll be glad to hear. Thanks.

The 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>
like image 805
Or Nevo Avatar asked Nov 09 '22 07:11

Or Nevo


1 Answers

First of all what you are actually doing in the cleanDots function is implementing the JavaScript replace() method, this function should be 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>

Beside this your code will execute finely.

like image 187
cнŝdk Avatar answered Nov 14 '22 22:11

cнŝdk