gpt4 book ai didi

javascript - Bad Word Catcher Javascript

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:40:59 26 4
gpt4 key购买 nike

我正在尝试创建一个使用文本字段从用户那里获取输入的应用程序。将字符串拆分为空格并查找字符串中的坏词。如果发现错误词,则在 DOM 上输出结果。

我一直在寻找表格,但找不到。我找到了一些 PHP 的,但这对我没有帮助。我相信我有正确的伪代码。一些指导会有所帮助。

HTML 如下:

<body>

<input type="text" id="wordInput"/>
<button id="badWordCatch" onclick="badWordCatch;">Catch Bad Words</button>
<div id="wordsFound"></div>
<div id="wordAmount"></div>
</body>

Javascript 如下:

    1. What words are put it
2. if the words are bad or not
*/

function badWordCatch(){

var wordInput = document.getElementById("wordInput").value;

// split the words by spaces (" ")
var arr = wordInput.split(" ");
// bad words to look for
var badWords = ["legos", "cloud", "manifold"];


//find bad words from the input
//output on the dom "if bad words were found"
//output on the dom how many bad words were found
}

最佳答案

您可以在 arr 上使用 .filter 来查看它是否包含来自 badWords 的任何单词。

function badWordCatch() {

var wordInput = document.getElementById("wordInput").value;
wordInput = wordInput.toLowerCase();

// split the words by spaces (" ")
var arr = wordInput.split(" ");
// bad words to look for, keep this array in lowercase
var badWords = ["legos", "cloud", "manifold"];

// .toLowerCase will do the case insensitive match!
var foundBadWords = arr.filter(el => badWords.includes(el));

document.getElementById("wordsFound").innerHTML = foundBadWords.join(", ");
document.getElementById("wordAmount").innerHTML = foundBadWords.length;


}
<input type="text" id="wordInput" value="legos happy manifold" />
<button id="badWordCatch" onclick="badWordCatch()">Catch Bad Words</button>
<div id="wordsFound"></div>
<div id="wordAmount"></div>

关于javascript - Bad Word Catcher Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48837569/

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