简体   繁体   English

Function 接受输入文本,检查它是否具有某些元素,并返回 output 值 (JS)

[英]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 的新手,我希望有人能帮我弄清楚如何开发一个应用程序:

  1. Takes a text value from an HTML input when pressing a button;按下按钮时从HTML 输入中获取文本值;
  2. Check if that text value has certain syllables (eg if it has the syllables "ba", "ca", and "da" in it);检查该文本值是否包含某些音节(例如,它是否包含音节“ba”、“ca”和“da”);
  3. returns a value to the HTML output whether or not it has syllables.HTML output返回一个值,无论它是否有音节。

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.

相关问题 检查输入值是否为回文的 Function 始终返回 true - Function that checks whether input value is palindrome or not always returns true Function 接受一个数组并返回 js 中旋转的 n 个空格内的元素 - Function that takes an array and returns elements inside rotated n spaces in js JS - 更改特定文本输入字段的值(类有几个输入元素) - JS - Change value of specific text-input-field (class has several input elements) 在一个简单的函数中反应 js 错误,该函数接受一个数组并返回一个随机值 - React js error in a simple function that takes an array and returns a random value Function 将缩写作为输入并返回全名字符串 - Function that takes an abbreviation as input and returns fullname string "对函数进行更改,该函数将序列作为参数并返回一个项目列表,其中没有任何具有相同值的元素" - Changes to make for the function which takes as argument a sequence and returns a list of items without any elements with the same value JS函数将一个对象作为输入并返回一个对象,该对象对作为参数传递的对象执行操作 - JS function takes an object as input and returns an object that performs actions on the object passed as a parameter 接受输入并具有if函数的HTML表单 - HTML form which takes the input and has an if function 如果长度超过一定数量的字符(VUE.JS),则禁用已动态创建的某个文本输入 - Disable a certain text input that has been created dynamically if the length is over a certain amount of chars(VUE.JS) 接受通用参数并返回通用值的函数 - Function that takes generic arguments and returns generic value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM