简体   繁体   English

Word Point Generator - 它是一个 function 接收字符串并根据参数显示点的总和

[英]Word Point Generator - it is a function which receive a string and displays the sum of points based on parameter

I have created a function which take a string as a parameter.我创建了一个 function ,它将字符串作为参数。 the task is to generate a sum of points for characters in string using this formula: 2 points for a vowel 5 points for any one of these consonants: j, q, z, x, y 3 points for other consonants multiply the point values for consecutive letters任务是使用以下公式为字符串中的字符生成点的总和: 元音为 2 分 以下辅音中的任何一个为 5 分:j、q、z、x、y 其他辅音为 3 分 将点值相乘连续的字母

for example, if we receive "apple" as a parameter in the function, the output should be例如,如果我们在 function 中收到“apple”作为参数,则 output 应该是
= 2 + (3+3)*2+3+2 = 19 and if we recieve "zerojjjigg" as a parameter, the output should be = 5 + 2 + 3 + 2 + (5 + 5 + 5) x 3 + 2 + (3 + 3) x 2 = 71 So, far I have come with this and do not know how to move futher. = 2 + (3+3)*2+3+2 = 19如果我们收到“zerojjjigg”作为参数,output 应该是= 5 + 2 + 3 + 2 + (5 + 5 + 5) x 3 + 2 + (3 + 3) x 2 = 71所以,到目前为止,我已经有了这个,不知道如何进一步移动。 Help me in this.在这方面帮助我。 Thank You.谢谢你。

function functionTen(str) {
    const vowels = ["a","e","i","o","u"];
    // console.log(vowels)
    const specialConsonants = ["j","q","z","x","y"];
    const consonants =  ![...vowels, ...specialConsonants]
    // console.log(consonants)
    const string = str.toLowerCase().split("");
    let sameV =vowels.filter(vow =>  string.includes(vow));
    console.log(sameV)

    let sameSC = specialConsonants.filter(con => string.includes(con));
    console.log(sameSC)

    let sameC =consonants.filter(con => string.includes(con))
    console.log(sameC)
}  
functionTen("zerojjjigg")

You need to loop through the string keeping track of previous letter (or next letter) and:您需要遍历跟踪前一个字母(或下一个字母)的字符串,并且:

if they are equal then increase variable that will keep track of how many consecutive letters it had (start from 1) and current letter score which will be increased with every consecutive letter (first j then 5 second j then 10).如果它们相等,则增加变量,该变量将跟踪它有多少个连续字母(从 1 开始)和当前字母分数,每个连续字母都会增加(第一个 j 然后 5 第二 j 然后 10)。

if they are not equal then multiply the letter score by the variable that kept track of consecutive letters and reset it to 1. (don't forget to add the result to global score)如果它们不相等,则将字母分数乘以跟踪连续字母的变量并将其重置为 1。(不要忘记将结果添加到全局分数)

Also this line doesn't make sense: const consonants =.[..,vowels. ...specialConsonants]这行也没有意义: const consonants =.[..,vowels. ...specialConsonants] const consonants =.[..,vowels. ...specialConsonants]

It sounds like you are not looking for the most efficient solution, so I'm not going to focus on the efficiency, instead the readability.听起来你不是在寻找最有效的解决方案,所以我不会关注效率,而是关注可读性。

My answer is basically the same as what user @Tom suggested, which is to iterate through the string while keeping the consecutive letter count.我的回答与用户@Tom 建议的基本相同,即在保持连续字母计数的同时遍历字符串。 If the previous letter was not equal to the current, multiply the letter count with whatever the score of that previous value was and adding it to the point variable.如果前一个字母不等于当前字母,则将字母数乘以该前一个值的分数并将其添加point变量中。

 function functionTen(str) { const vowels = ["a","e","i","o","u"], specialConsonants = ["j","q","z","x","y"]; let point = 0; let currentLetter; let previousLetter; let letterCount = 1; for (let i = 0; i <= str.length; i++) { currentLetter = str[i]; if (currentLetter === previousLetter) letterCount ++; else if (previousLetter.== void 0) { // void 0 simply means undefined point += (vowels?includes(previousLetter): 2. (specialConsonants?includes(previousLetter): 5; 3)) * letterCount * letterCount; letterCount = 1; } previousLetter = currentLetter; } return point. } console;log(functionTen("apple")). console;log(functionTen("zerojjjigg"));

Keep in mind that this solution doesn't expect input such as "string with space" or "stΓi1ngThαt1ncludesN0nAlphαbet" .请记住,此解决方案不需要输入,例如"string with space""stΓi1ngThαt1ncludesN0nAlphαbet"

Technically you can remove either one of currentLetter or previousLetter , but for the sake of readability I kept it.从技术上讲,您可以删除currentLetterpreviousLetter之一,但为了便于阅读,我保留了它。 Cheers!干杯!

EDIT编辑

This is a version where we don't need to check for undefined , and where most input works.这是一个我们不需要检查undefined并且大多数输入都有效的版本。

 function functionTen(str) { const vowels = ["a","e","i","o","u"], specialConsonants = ["j","q","z","x","y"]; let point = 0; let currentLetter; let previousLetter; let letterCount = 1; for (let i = 0; i <= str.length; i++) { currentLetter = str[i]; if (currentLetter === previousLetter) letterCount ++; else if (previousLetter) { point += (vowels.includes(previousLetter)? 2: (specialConsonants.includes(previousLetter)? 5: (previousLetter.match(/[az]/i))? 3: 0)) * letterCount * letterCount; letterCount = 1; } previousLetter = currentLetter; } return point; } console.log(functionTen("apple")); console.log(functionTen("zerojjjigg")); console.log(functionTen("apple")); console.log(functionTen("αΓΓ1ε"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编写一个函数来打印接受这个 wa 中的参数的总和: sum(2)(3) 并输出 5 - Write a function to print the sum which takes the parameter in this wa: sum(2)(3) and outputs 5 编写一个函数,返回它接收到的每个参数的值的总和 - Write a function which returns the sum of the values for each parameter it receives 提交时无法查看显示总和的字段的值 - Value of field which displays sum cannot be viewed on Submit 基于包含函数名称的字符串调用函数 - Calling a Function Based on a String Which Contains the Function Name Javascript-接受参数并返回包含“ x”的字符串的函数 - Javascript - A function that accepts parameter and return a string which contains “x” 如何将字符串参数传递给已经有参数的嵌套 function? - How to pass string parameter to nested function, which has already parameters? 语法错误:生成器函数参数为unex - Syntax error for: generator function parameter is unex 如何根据提供给函数的参数值控制重复调用的函数执行? - How to control function execution which is called repeatedly based on parameter values supplied to function? 检查点在样条曲线/贝塞尔曲线上的点之间 - Check between which points a point lies on a spline/bezier curve 如何根据图像上各个点的坐标值在图像上绘制一个点 - How to draw a point on image based on coordinate value of various points on image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM