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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With