gpt4 book ai didi

JavaScript 错误 : Uncaught TypeError: undefined is not a function on line 4; I Don't Know What I Did Wrong

转载 作者:行者123 更新时间:2023-12-02 16:32:38 26 4
gpt4 key购买 nike

我试图让用户写一个句子,然后让计算机在单独的“确认窗口”中确认每个单词,而不使用空格。为了确定此代码是否是执行此任务的正确代码,我必须运行该代码,但是我收到第四行标题中列出的错误。首先,我不明白该错误在说什么。如果它告诉我第四行的 if 语句不是函数并且无法运行它,那是怎么回事?

<script>
function sentenceFinder (sentence){
for (counter = 0; counter < sentence.length; counter++){
if (sentence.substring(counter, counter + 1) !== " "){
words.push(sentence.substring(counter, counter + 1));
}
else {
comfirm(words[0]);
}
};
}

var x = prompt("Please type in the sentence that will be seperated.")
sentenceFinder(x)

var words = []
</script>

最佳答案

您有两个问题导致错误。

  1. 您正在使用 subscript,我认为您打算使用 substring
  2. 在将其设置为数组之前,您正在使用 words

我想这就是你想要写的。

function sentenceFinder (sentence){
for (counter = 0; counter < sentence.length; counter++){
if (sentence.substring(counter, counter + 1) !== " "){
words.push(sentence.substring(counter, counter + 1));
}
else {
comfirm(words[0]);
}
};
}

var words = [];
var x = prompt("Please type in the sentence that will be seperated.");
sentenceFinder(x);

关于JavaScript 错误 : Uncaught TypeError: undefined is not a function on line 4; I Don't Know What I Did Wrong,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28164004/

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