gpt4 book ai didi

javascript - 来自输入的字符串和运行时的字符串不相等?

转载 作者:搜寻专家 更新时间:2023-11-01 04:36:09 25 4
gpt4 key购买 nike

所以我正在尝试阅读 <textarea>输入,将其分成单独的单词,然后检查每个单词是否等于关键字列表中的单词。

我已经完成了代码,当我测试时,它没有工作。当我调试它时,我看到当它比较来自 <textarea> 的字符串时和一个看起来相同的常量字符串,它认为它们不相等。

我试图从 <textarea> 中获取字符串(一模一样,复制粘贴)和常量字符串静态比较,结果为真。

如果您知道可能是什么问题,我将很高兴听到。谢谢。

代码:

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>

最佳答案

首先,您在 cleanDots 函数中实际执行的操作是实现 JavaScript replace() 方法,该函数应如下所示:

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>

除此之外,您的代码将执行良好。

关于javascript - 来自输入的字符串和运行时的字符串不相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32072857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com