[英]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.