javascript - 如何创建以不同于辅音的方式对元音进行评分的字母评分函数

标签 javascript node.js

所以我正在尝试创建一个单词评分算法,为每个辅音打一分,为每个元音打三分。但是我尝试过的每一次尝试都失败了,这意味着某些事情并没有让我在精神上点击,我需要帮助来理解我做错了什么

我在整个网络上进行了搜索,找到了多种检查单词字母和计算元音的方法,我尝试自定义这些方法并将其用于我的评分算法,但在测试每种方法时我仍然得到错误的评分输出.我研究过使用迭代方法和使用正则表达式。有人告诉我我的错在我的第二个循环中,但我不确定如何纠正它。

//The attempt where apparently the second loop is the problem: 
vowelBonus= (word)=>{
  let score = 0;
  const vowels = ["a","e","i","o","u"];
  word = word.toLowerCase();
  for (letter in word){
    vowels.forEach((value)=>{
      if (letter === value){
        score += 3
      }else if(letter !== value){
        score += 1
      }
    })
    return score
  }
}

// An attempt using a regular expression which I kind of prefer after reading up on them

vowelBonus = (word)=>{
  let vowel = word.match(/[aeiou]/gi);
  let vCount = 0
  let consCount = 0
  let score = vCount + consCount
  if(vowel.length){
    vCount += 3
  }else if (!vowel.length){
    consCount =+ 1
  }
  return score
}

//I have about 3-5 other versions I've toyed around with but I'd like to stay in the ballpark of something like the two snippets I've pasted above. 

//One version keeps giving an output of 0. Another gives increments of 5, and I've had a few give a result of 1-5. I'm expecting the result to be 1pt for every consonant and 3pts for every vowel. Meaning if I type in the word "code", the result should be 8pts.

最佳答案

编辑:不要忘记边缘情况,比如如果有人在单词中提供了一个空格……只是需要考虑的事情……

编辑 2: 添加了使用 regex 的示例,该示例忽略提供的空格..

像这样的东西应该可以工作。我发现使用 Array.includes 方法最简单。

如果您有任何问题,请告诉我!

function scoreWord(word) {
  // Ignores spaces
  return word.replace(/\s/g,'').toLowerCase().split("").reduce((a, i) => 
    a += ["a", "e", "i", "o", "u"].includes(i) ? 3 : 1, 0);
}


function scoreWordSimplified(word) {
  // Ignores spaces; Simplified example
  let wordArray = word.replace(/\s/g, "").toLowerCase().split("");
  let vowels = ["a", "e", "i", "o", "u"];
  let pointsForVowel = 3;
  let pointsForConsonant = 1;
  let finalScore = 0;

  wordArray.forEach(letter => {
    if (vowels.includes(letter)) {
      finalScore += pointsForVowel;
    } else {
      finalScore += pointsForConsonant;
    }
    /** Or you could do:
     * finalScore += vowels.includes(letter) ? pointsForVowel : pointsForConsonant;
     */
  });

  return finalScore;
}


let vowelBonus = (word)=>{
  // credit: @Code Maniac
  // Ignores spaces
  let _word = word.replace(/\s/g,'');
  let vowel = _word.match(/[aeiou]/gi);
  let wordLength = _word.length;
  let vowelLength = vowel && vowel.length;
  return vowel ? (wordLength - vowelLength) + vowelLength *3 : wordLength
}

let score1 = scoreWord("some word");
let score2 = vowelBonus("some word");
let score3 = scoreWordSimplified("some word");

console.log("scoreWord: ", score1);
console.log("vowelBonus: ", score2);
console.log("scoreWordSimplified: ", score3);

关于javascript - 如何创建以不同于辅音的方式对元音进行评分的字母评分函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57940672/

相关文章:

javascript - 将选定的值从文本框移动到下拉列表

javascript - 在 NodeJS 中包含 Javascript 文件,如 PHP

javascript - 在 jsdom 中加载 Google Maps API

javascript - Passport JS "Can' t 在发送后设置标题"

javascript - 在 Node 流中找不到 mp3 帧头同步位

javascript - 如何从 e.target.name 更新对象状态

javascript - 将命令行参数传递给 emscripten 生成的应用程序

javascript - Bluebird promise 订单问题

javascript - Sequelize 包含模型但没有表名

mongodb - mongoosejs 有什么好的引用吗?