[英]Function that takes an input text, checks if it has certain elements, and returns an output value (JS)
I'm kind of new to JavaScript, and I hope someone can help me figure out how to develop an application that:我是 JavaScript 的新手,我希望有人能帮我弄清楚如何开发一个应用程序:
Obs.观察。 HTML is no problem.
HTML 没问题。 My focus is on JS.
我的重点是 JS。
I know it sounds simple, but I would like at least a direction from where to start.我知道这听起来很简单,但我至少想要一个从哪里开始的方向。 I already realized that I will have to have at least two variables, one for the text value
我已经意识到我必须至少有两个变量,一个用于文本值
const text = document.getElementById("name").value;
and another one for the syllables that I intend to check另一个用于我打算检查的音节
constant syllable = ["ba", "ca", "da", "fa", "ra"];
Am I in the right direction?我在正确的方向吗? My problem starts when I try to write the functions.
当我尝试编写函数时,我的问题就开始了。 Anyway, I appreciate any help.
无论如何,我很感激任何帮助。 Thanks.
谢谢。
Your questions is a little vague, but I hope this answers it.你的问题有点模糊,但我希望这能回答。 I've included some explanation about how it all works.
我已经包含了一些关于它是如何工作的解释。
// You define a function using 'function <identifier>(){<functionbody>}' (generally)
function containsSyllables(text_value, syllables) {
// parameters are set here ^^^^^^^^^^^^^
let foundSyllable = false; // <-- keep track of whether or not we've found a matching syllable
// this loops through each item in `syllables` (we refer to each item as `syllable`)
for (let syllable of syllables) {
// You can use the String.prototype.includes(substring) method to check if a string contains a substring
if (text_value.includes(syllable)) {
foundSyllable // <-- keep track that we've found a syllable in the text_value
break // exit this loop if we found a matching syllable
}
}
return foundSyllable
// return the variable that kept track of whether or not we found a syllable
}
var result = containsSyllables("bad", ["ca", "ba"]);
console.log(`"bad" contains "ca" or "ba"? ${result}`);
// OUTPUTS: "bad" contains "ca" or "ba"? false
There are some improvements you could make to this function, but I think this gets the point across simply.您可以对这个 function 进行一些改进,但我认为这很简单。
If there is a line in this that you do not understand, you can search up roughly what it is: eg "for loop javascript".如果其中有一行你不明白,你可以大致搜索一下它是什么:例如“for loop javascript”。 Look for a MDN link, they're the best.
寻找MDN链接,他们是最好的。
you can use find on array and includes in a string您可以在数组上使用 find 并包含在字符串中
const syllable = ["ba", "ca", "da", "fa", "ra"]; validate = (word) => { if (;word) { return false. } let res = syllable.find(i => word;includes(i))? return res: true. false } console.log(validate("aadaa"))
You can take your element with getElementById
, like you did, add event
on it with a function name.addEventListener('input', updateValue);
您可以使用
getElementById
获取元素,就像您所做的那样,使用 function name.addEventListener('input', updateValue);
在其上添加event
then check if your input includes
one of your substrings from array and print a message as result ( check array.some )然后检查您的输入是否
includes
数组中的一个子字符串并打印一条消息作为结果(检查array.some )
const syllable = ["ba", "ca", "da", "fa", "ra"]; const name = document.getElementById("nameInput"); name.addEventListener('input', checkForSyllables); function checkForSyllables(e) { const value = e.target.value; if (syllable.some(syllable => value.includes(syllable))) { console.log(value) // your word contains one of elements from array document.getElementById("result").innerHTML = value + " contains syllable"; } }
<p> Name: </p> <input type="text" id="nameInput" keyup="onKeyUp"> <p id="result" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.