简体   繁体   中英

Find the longest word in string

I'm trying to solve this task - "given a string, return the word with the highest score. The score can be calculated the following way:

  • for each vowel, add +1 to the score (["a", "e", "i", "o", "u", "y"])
  • for each constant ["b", "d"] substract 1 from the score

for example, "butterfly" will have the score of 2, we add +3 for each vowel ("u","e","y") and substract 1 for constant ("b"). The word "day" will have score of 2 ( we add 2 for "a","y"). The function needs to return the word with the highest score.

This is my code so far:

function countVowels(string) {
  let vowels = ["a", "e", "i", "o", "u"] 
  let constants = ["b", "d"]

  let words = string.trim().split(" ")

  let highestScore = " ";
  let count =0;
  
  for(const c of words){
    for (let i=0; i<c.length; i++){

        if(vowels.includes(c[i])) {
        highestScore = c
        count++;
        }
        else if (constants.includes(c[i])) count--    
    }
}
console.log(highestScore)
return highestScore;

}

countVowels("what a beautiful day")

The issue is that my code returns the last word in the sentence, not the word with the highest score. If I pass "what a beautiful day", function returns "day" not "beautiful". I can't seem to find a way to return the correct value. Any advise and help is appreciated.

You need another variable to keep track of the score of one word you're iterating over, separate from the highest score found so far. More precise variable names will help some too - highestScore sounds like a number (a score), but your current code has it as a string. Consider using something like bestWordFoundSoFar .

 function countVowels(string) { const vowels = ["a", "e", "i", "o", "u"] const constants = ["b", "d"] const words = string.trim().split(" ") let bestWordSoFar; let scoreOfBestWordSoFar = 0;; for (const word of words) { let thisWordScore = 0; for (const char of word) { if (vowels.includes(char)) thisWordScore++; else if (constants.includes(char)) thisWordScore-- } if (thisWordScore > scoreOfBestWordSoFar) { bestWordSoFar = word; scoreOfBestWordSoFar = thisWordScore } } return bestWordSoFar; } console.log(countVowels("what a beautiful day"));

A somewhat more functional approach can be had with .reduce , to keep track of and return the best word found.

 function countVowels(string) { const vowels = ["a", "e", "i", "o", "u"]; const constants = ["b", "d"]; const getWordScore = (word) => { let thisWordScore = 0; for (const char of word) { if (vowels.includes(char)) thisWordScore++; else if (constants.includes(char)) thisWordScore-- } return thisWordScore; }; return string.trim().split(" ").reduce((bestWordSoFar, word) => getWordScore(word) > getWordScore(bestWordSoFar)? word: bestWordSoFar); } console.log(countVowels("what a beautiful day"));

You're not resetting the score ( count ) to individualize it to each word. You can use Array#map to do that as follows:

 function countVowels(string) { let vowels = ["a", "e", "i", "o", "u"] let constants = ["b", "d"] let words = string.trim().split(" ") const scores = words.map(word => { let score = 0; for (let i=0; i < word.length; i++){ if(vowels.includes(word[i])) { score++ } else if (constants.includes(word[i])) score-- } console.log( word, score ); //output each word and score return [word, score]; }).sort((a,b) => b[1] - a[1]); //sort the array in descending order of score return scores[0][0]; //return the WORD with highest score } console.log('Word with highest score: ' + countVowels("what a beautiful day") );

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