我正在使用 javascript 尝试制作一个字符串单词搜索器。但是,当我运行代码时,我得到 TypeError: text.indexOf is not a function。我不认为存在语法错误,或者也许我完全遗漏了一些东西。
var text = prompt("Paste Text Here").toUpperCase;
var word = prompt("What word would you like to search for?").toUpperCase;
var hits = [];
var n = text.indexOf(word);
hits.push(text[n]);
if (hits.length === 0) {
console.log("Your word could not be found.");
} else {
console.log("Your word was found " + hits.length + " times.");
}
最佳答案
var text = prompt("Paste Text Here").toUpperCase;
你应该调用函数
var text = prompt("Paste Text Here").toUpperCase()
关于javascript - 类型错误 : text. indexOf 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33991771/