[英]How to Combine Two Related Functions (Javascript)
标题:得分最高的词
信息:我很难让这两个代码片段只在一个 function 中工作。 目前,第一个代码片段只是将句子拆分为单个单词和字母 arrays。 第二个代码片段是我希望第一个对每个单独的句子-单词-字母数组执行的操作。
问题:我怎样才能让这两个函数一起工作,以便它们返回具有最高索引值总和的单词。
返回:这些函数组合起来将返回句子中每个单词的总和,以确定哪个单词的总和值最高。
示例:如果句子是“aa bb”,那么,
"aa" < "bb" 因为 "aa" === 2 和 "bb" === 4,所以这个 function 将返回单词 "bb",因为 "aa" 小于 "bb"。
我的尝试(与信息有关):
片段#1:
(将每个单词拆分为其单个单词字符)
//Snippet #1 function high(x){ const alph = "abcdefghijklmnopqrstuvwxyz"; const ltrArr = x.split(" ").map((w) => { return w.split("") }) return ltrArr } console.log(high('this is a test sentence')) //-- returns word/letter array
片段#2:
(对于每个句子单词,将每个单词拆分为单独的字母并总结总索引值)
//Snippet #2 const alph = "abcdefghijklmnopqrstuvwxyz"; const firstWordLtrs = ['t','h','i','s']; //the first word of the sentence split up into a character array. const sum = firstWordLtrs.map((l) => { //finds the index of the individual letter in the alphabet and sums up the total. const index = alph.indexOf(l)+1 return index }).reduce((a,b) => a+b) console.log(sum) //--returns 56
最后:这两个函数一起返回句子中每个单词的单独索引总和。 (例如,为句子“aa bb”返回“bb”)
附言。 我不知道给这个问题取什么标题。
谢谢!
我可能会误解,但这就是你要找的吗?
const alph = "abcdefghijklmnopqrstuvwxyz"; const sum = (sentence) => { let highest = { word: "", score: 0 } sentence.split(" ").forEach(w => { let s = 0; w.split("").forEach(l => { s += (alph.indexOf(l) + 1); }) if (s > highest.score) highest = { word: w, score: s }; }) return highest; } console.log(sum('this is a test sentence')) //-- returns word/letter array
您可以使用这样的链式方法。
const sum = high('your sentece here').map((l) => { //finds the index of the individual letter in the alphabet and sums up the total.
const index = alph.indexOf(l)+1
return index
}).reduce((a,b) => a+b)
我删除了第一个 function 中的嵌套 arrays ,因为它们并没有真正发挥作用。 让我知道这是不是故意的。
否则,这会将组合功能提供到单个 function 中:
function high(x) { const alph = "abcdefghijklmnopqrstuvwxyz"; const ltrArr = x.replace(" ", "").split("") const sum = ltrArr.map((l) => alph.indexOf(l) + 1).reduce((a,b) => a+b); return sum; } console.log(high('this is a test sentence')) // returns the index sum of the string
let findHighestWord = sentence => { let words = sentence.split(' '); const alpha = 'abcdefghijklmnopqrstuvwxyz'; let scores = words.map(word => [...word].map(char => alpha.indexOf(char) + 1).reduce((a, b) => a + b)); let index = scores.indexOf(Math.max(...scores)); return {word: words[index], index: index, score: scores[index]}; }; let sentence = 'this is a zzzz sentence'; console.log(findHighestWord(sentence));
以前的答案(在更新问题之前)以防它对任何人有用:
其他答案似乎将您的问题解释为将句子映射到单个分数。 我认为您实际上是在问如何将 map 句子转换为分数数组,句子中每个单词 1 分。
以下是如何将 2 个片段重新排列为 2 个函数并将它们一起使用:
let splitSentence = sentence => sentence.split(' ').map(word => [...word]); let sumWord = word => { const alpha = 'abcdefghijklmnopqrstuvwxyz'; return word.map(char => alpha.indexOf(char) + 1).reduce((a, b) => a + b); }; let sentence = 'this is a sentence'; let wordSums = splitSentence(sentence).map(sumWord); console.log(wordSums);
如果这是您正在寻找的,那么您可以将这两个功能组合成 1 function:
let splitSentenceAndSumWords = sentence => { let words = sentence.split(' ').map(word => [...word]); const alpha = 'abcdefghijklmnopqrstuvwxyz'; return words.map(word => word.map(char => alpha.indexOf(char) + 1).reduce((a, b) => a + b)); }; let sentence = 'this is a sentence'; console.log(splitSentenceAndSumWords(sentence));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.