gpt4 book ai didi

javascript - Uncaught ReferenceError : Invalid left-hand side in assignment. JavaScript

转载 作者:行者123 更新时间:2023-12-03 05:01:03 25 4
gpt4 key购买 nike

我想用另一个字符串替换字符串(原始)中的字符。
我在运行调试器时遇到错误。
我不明白语法有什么问题。

<!DOCTYPE>
<html>

<head>
<title>HI there</title>
<meta lang="english">
</head>

<body>
<div>
Enter the original string<input id="original" value="" type="text"> <br>
Enter the replacing string<input id="replacing" value="" type="text"><br>
Enter the location to be replaced<input id="tobereplaced" value="" type="text"><br>
</div>
<br>
<button type="submit" onclick="replace()">Submit</button>
<br> Here you go the replaced string is:

<script>
function replace() {
var original = document.getElementById("original").value;
var replacing = document.getElementById("replacing").value;
var tobereplaced = document.getElementById("tobereplaced").value;
var replaced = "";
var originalLength = original.length;
var tobereplacedLength = tobereplaced.length;
var k = 0;
for (var i = 0; i < originalLength; i++) {
replaced.charAt(k) = original.charAt(i)
if (original.charAt(i) == replacing.charAt(0)) {
replaced = replaced + tobereplaced;
k = k + tobereplacedLength;
i++;
}
k++;
}
document.getElementById("replaced").innerHTML = replaced;

}
</script>
<h1 id="replaced"></h1>
</body>

</html>

最佳答案

您正在尝试更改第 28 行处的空字符串的字符 [replaced.charAt(k) = original.charAt(i)] 这就是问题所在。代码中还存在一些不需要的增量。请在下面找到更正的内容

我已经用//注释代码更新了下面的代码并添加了正确的代码。它的工作原理

        // var k = 0; //Commented
// debugger; //Commented
for (var i = 0; i < originalLength; i++) {

if (original.charAt(i) == replacing.charAt(0)) {
replaced = replaced + tobereplaced;
// k = k + tobereplacedLength; //Commented
// i++; //Commented
} else{
replaced = replaced + original.charAt(i);
}
// k++; //Commented
}

关于javascript - Uncaught ReferenceError : Invalid left-hand side in assignment. JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42224689/

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