简体   繁体   English

如何组合两个相关函数(Javascript)

[英]How to Combine Two Related Functions (Javascript)

Title: Highest Scoring Word标题:得分最高的词

Info: I'm having a hard time getting these two code snippets to work in just one function.信息:我很难让这两个代码片段只在一个 function 中工作。 Currently, the first code snippet just splits up the sentence into individual word and letter arrays.目前,第一个代码片段只是将句子拆分为单个单词和字母 arrays。 The second code snippet is what I want the first to do with each individual sentence-word-letter array.第二个代码片段是我希望第一个对每个单独的句子-单词-字母数组执行的操作。

Question: How can I get these two functions to work together so that they return the word with the highest index value sum.问题:我怎样才能让这两个函数一起工作,以便它们返回具有最高索引值总和的单词。

Return: These functions combined will return the sum of each word in the sentence to determine which word has the highest sum value.返回:这些函数组合起来将返回句子中每个单词的总和,以确定哪个单词的总和值最高。

Example: If the sentence is "aa bb" then,示例:如果句子是“aa bb”,那么,

"aa" < "bb" because "aa" === 2 and "bb" === 4, so this function will return the word "bb", because "aa" is less than "bb". "aa" < "bb" 因为 "aa" === 2 和 "bb" === 4,所以这个 function 将返回单词 "bb",因为 "aa" 小于 "bb"。


My Attempt ( related to info ):我的尝试与信息有关):

Snippet #1:片段#1:

(splits up each word for its individual word characters) (将每个单词拆分为其单个单词字符)

 //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

Snippet #2:片段#2:

(for each of the sentences words, split each word into its individual letters and sum up the total index values) (对于每个句子单词,将每个单词拆分为单独的字母并总结总索引值)

 //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

Finally: Both these functions together will return the individual index sum for each word in the sentence.最后:这两个函数一起返回句子中每个单词的单独索引总和。 (eg. return "bb" for sentence "aa bb") (例如,为句子“aa bb”返回“bb”)

ps.附言。 I wasn't sure what to title this question.我不知道给这个问题取什么标题。

Thank you!谢谢!

I might be misunderstanding, but is this what you were looking for?我可能会误解,但这就是你要找的吗?

 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

You can utilize chain methods like this.您可以使用这样的链式方法。

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)

I removed the nested arrays in the first function since they didn't really serve a purpose.我删除了第一个 function 中的嵌套 arrays ,因为它们并没有真正发挥作用。 Let me know if it was intentional.让我知道这是不是故意的。

Otherwise, this provides the combined functionality into a single function:否则,这会将组合功能提供到单个 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));

Previous answer (before question was updated) in case it's useful to anyone:以前的答案(在更新问题之前)以防它对任何人有用:

The other answers seem to interpret your question as mapping a sentence to a single score.其他答案似乎将您的问题解释为将句子映射到单个分数。 I think you're actually asking how to map the sentence to an array of scores, 1 score for each word in the sentence.我认为您实际上是在问如何将 map 句子转换为分数数组,句子中每个单词 1 分。

Here's how you could rearrange your 2 snippets into 2 functions and use them together:以下是如何将 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);

And here's how you could combine the 2 functions into 1 function if that's what you're looking for:如果这是您正在寻找的,那么您可以将这两个功能组合成 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM