javascript - 类型错误 : text. indexOf 不是函数

标签 javascript indexof

我正在使用 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/

相关文章:

javascript - 我怎样才能在点击时制作菜单下拉菜单

c# - 使用linq获取列表c#中部分匹配项的索引

javascript - 如何使用 Angular 获取数组中对象的索引?

c# - 如何在不同的字符串中找到一个字符串的所有索引?

c# - SVG - 获取两条路径的差异路径

javascript - 获取随机不重复颜色

javascript - jQuery:查找具有特定自定义属性的元素

java - 动态构建数据表与脚本

C# 如何在字符串中写行特定字符串?

javascript - 文本文件行与字符串 indexOf() 不匹配