简体   繁体   中英

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. Currently, the first code snippet just splits up the sentence into individual word and letter 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" because "aa" === 2 and "bb" === 4, so this function will return the word "bb", because "aa" is less than "bb".


My Attempt ( related to info ):

Snippet #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:

(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")

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. Let me know if it was intentional.

Otherwise, this provides the combined functionality into a single 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.

Here's how you could rearrange your 2 snippets into 2 functions and use them together:

 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:

 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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