我真的被这个递归函数问题困住了,我需要 return 一个对象中数量最多的元音
I'm really stuck on this recursive function question, I need to return the vowel with the greatest count in an object
const VOWELS = ['a', 'e', 'i', 'o', 'u'];
const mostFrequentVowel = function (words, counter = {}) {
let count = 0
if (words.length === 0) {
return ''
}
let lastString = words[words.length - 1]
for (let i = 0; i < lastString.length; i++) {
const letter = lastString[i];
if (VOWELS.includes(letter)) {
count++
if (letter in counter) {
counter[letter] += 1
} else {
counter[letter] = 1
}
}
}
let values = Object.values(counter)
let max = Math.max(...values)
for (let vowel in counter) {
if (counter[vowel] === max) {
return vowel
}
}
return mostFrequentVowel(words.slice(0, words.length - 1), counter)
}
console.log(mostFrequentVowel(['dog', 'cow', 'pig', 'chicken', 'horse'])); // 'o'
mostFrequentVowel(['dog', 'cow', 'pig', 'chicken']); // 'i' or 'o'
我在更新计数器对象以反映最大元音时遇到问题。我想知道是否更多的是 for in 循环接近尾声的问题,或者它是否在第一个 for 循环中有一些逻辑。
问题是只有当单词长度 === 1(列表中剩余的最后一个单词)时,才应调用第一个 return。
代码的编写方式是,只要有元音,第一个 return 就会被调用。这个 if 语句可以解决这个问题:
const mostFrequentVowel = function (words, counter = {}) {
let count = 0
if (words.length === 0) {
return ''
}
let lastString = words[words.length - 1]
for (let i = 0; i < lastString.length; i++) {
const letter = lastString[i];
if (VOWELS.includes(letter)) {
count++
if (letter in counter) {
counter[letter] += 1
} else {
counter[letter] = 1
}
}
}
if (words.length === 1) {
let values = Object.values(counter)
let max = Math.max(...values)
for (let vowel in counter) {
if (counter[vowel] === max) {
return vowel
}
}
}
else {
return mostFrequentVowel(words.slice(0, words.length - 1), counter);
};
};
希望这个回答对您的问题有所帮助
你必须使用递归吗???
如果不是:
const VOWELS = ['a', 'e', 'i', 'o', 'u'];
const mostFrequentVowel = function (words) {
const vowelCount = words.reduce((accumulator, word) => {
Array.from(word).forEach(character => {
if (VOWELS.includes(character)) {
accumulator = { ...accumulator, [character]: accumulator[character] ? accumulator[character] + 1 : 1 }
}
})
return accumulator
}, {})
return Object.keys(vowelCount).reduce((a, b) => vowelCount[a] > vowelCount[b] ? a : b)
}
console.log(mostFrequentVowel(['dog', 'cow', 'pig', 'chicken', 'horse'])); // 'o'
console.log(mostFrequentVowel(['dog', 'cow', 'pig', 'chicken'])); // 'i' or 'o'
改用这个:
const VOWELS = ['a', 'e', 'i', 'o', 'u'];
function mostFrequentVowel(inputs)
{
var max=0;
VOWELS.forEach(p=>max=Math.max(inputs.join().match(new RegExp(p, 'gm'))?.length ?? 0,max));
return VOWELS[max]; // <-Update | mistake -> inputs[max];
}
'use strict'
const mostFrequentVowel = function (words) {
if (words.length === 0) {
return null
}
const vowels = ['a', 'e', 'i', 'o', 'u'];
let vowelsCounter = {
'a': 0,
'e': 0,
'i': 0,
'o': 0,
'u': 0
};
const allWords = words.join('')
console.log(`allWords: ${allWords}`) // allWords: dogcowpigchickenhorse
const chars = allWords.split('');
console.log(`chars: ${chars}`) // chars: d,o,g,c,o,w,p,i,g,c,h,i,c,k,e,n,h,o,r,s,e
const allVowels = chars.filter(letter => vowels.includes(letter))
console.log(`allVowels: ${allVowels}`) // allVowels: o,o,i,i,e,o,e
const vowelsCounterFinal = allVowels.reduce( (acc, vowel) => { // acc ==> acumulator (the previous state) // currentOrder ==> current value in the loop
acc[vowel] = acc[vowel] + 1
return acc // REDUCER ==> Remembers the previous state // return the next state
},
vowelsCounter // DEFAULT TO START WITH
)
console.log(`vowelsCounterFinal: ${vowelsCounterFinal}`)
const keyWithTheHighestValueFromObject = Object.keys(vowelsCounterFinal).reduce((a, b) => vowelsCounterFinal[a] > vowelsCounterFinal[b] ? a : b);
// return keyWithTheHighestValueFromObject // return only the vowel
return {
vowel: keyWithTheHighestValueFromObject,
count: vowelsCounter[keyWithTheHighestValueFromObject]
} // return the vowel and the count
console.log('Debigging')
}
const wordsArray = ['dog', 'cow', 'pig', 'chicken', 'horse']
console.log(JSON.stringify(mostFrequentVowel(wordsArray), null, 2))
/*
{
"vowel": "o",
"count": 3
}
*/
const VOWELS = ['a', 'e', 'i', 'o', 'u'];
const mostFrequentVowel = function (words, counter = {}) {
let count = 0
if (words.length === 0) {
return ''
}
let lastString = words[words.length - 1]
for (let i = 0; i < lastString.length; i++) {
const letter = lastString[i];
if (VOWELS.includes(letter)) {
count++
if (letter in counter) {
counter[letter] += 1
} else {
counter[letter] = 1
}
}
}
let values = Object.values(counter)
let max = Math.max(...values)
for (let vowel in counter) {
if (counter[vowel] === max) {
return vowel
}
}
return mostFrequentVowel(words.slice(0, words.length - 1), counter)
}
console.log(mostFrequentVowel(['dog', 'cow', 'pig', 'chicken', 'horse'])); // 'o'
mostFrequentVowel(['dog', 'cow', 'pig', 'chicken']); // 'i' or 'o'
我在更新计数器对象以反映最大元音时遇到问题。我想知道是否更多的是 for in 循环接近尾声的问题,或者它是否在第一个 for 循环中有一些逻辑。
问题是只有当单词长度 === 1(列表中剩余的最后一个单词)时,才应调用第一个 return。 代码的编写方式是,只要有元音,第一个 return 就会被调用。这个 if 语句可以解决这个问题:
const mostFrequentVowel = function (words, counter = {}) {
let count = 0
if (words.length === 0) {
return ''
}
let lastString = words[words.length - 1]
for (let i = 0; i < lastString.length; i++) {
const letter = lastString[i];
if (VOWELS.includes(letter)) {
count++
if (letter in counter) {
counter[letter] += 1
} else {
counter[letter] = 1
}
}
}
if (words.length === 1) {
let values = Object.values(counter)
let max = Math.max(...values)
for (let vowel in counter) {
if (counter[vowel] === max) {
return vowel
}
}
}
else {
return mostFrequentVowel(words.slice(0, words.length - 1), counter);
};
};
希望这个回答对您的问题有所帮助
你必须使用递归吗??? 如果不是:
const VOWELS = ['a', 'e', 'i', 'o', 'u'];
const mostFrequentVowel = function (words) {
const vowelCount = words.reduce((accumulator, word) => {
Array.from(word).forEach(character => {
if (VOWELS.includes(character)) {
accumulator = { ...accumulator, [character]: accumulator[character] ? accumulator[character] + 1 : 1 }
}
})
return accumulator
}, {})
return Object.keys(vowelCount).reduce((a, b) => vowelCount[a] > vowelCount[b] ? a : b)
}
console.log(mostFrequentVowel(['dog', 'cow', 'pig', 'chicken', 'horse'])); // 'o'
console.log(mostFrequentVowel(['dog', 'cow', 'pig', 'chicken'])); // 'i' or 'o'
改用这个:
const VOWELS = ['a', 'e', 'i', 'o', 'u'];
function mostFrequentVowel(inputs)
{
var max=0;
VOWELS.forEach(p=>max=Math.max(inputs.join().match(new RegExp(p, 'gm'))?.length ?? 0,max));
return VOWELS[max]; // <-Update | mistake -> inputs[max];
}
'use strict'
const mostFrequentVowel = function (words) {
if (words.length === 0) {
return null
}
const vowels = ['a', 'e', 'i', 'o', 'u'];
let vowelsCounter = {
'a': 0,
'e': 0,
'i': 0,
'o': 0,
'u': 0
};
const allWords = words.join('')
console.log(`allWords: ${allWords}`) // allWords: dogcowpigchickenhorse
const chars = allWords.split('');
console.log(`chars: ${chars}`) // chars: d,o,g,c,o,w,p,i,g,c,h,i,c,k,e,n,h,o,r,s,e
const allVowels = chars.filter(letter => vowels.includes(letter))
console.log(`allVowels: ${allVowels}`) // allVowels: o,o,i,i,e,o,e
const vowelsCounterFinal = allVowels.reduce( (acc, vowel) => { // acc ==> acumulator (the previous state) // currentOrder ==> current value in the loop
acc[vowel] = acc[vowel] + 1
return acc // REDUCER ==> Remembers the previous state // return the next state
},
vowelsCounter // DEFAULT TO START WITH
)
console.log(`vowelsCounterFinal: ${vowelsCounterFinal}`)
const keyWithTheHighestValueFromObject = Object.keys(vowelsCounterFinal).reduce((a, b) => vowelsCounterFinal[a] > vowelsCounterFinal[b] ? a : b);
// return keyWithTheHighestValueFromObject // return only the vowel
return {
vowel: keyWithTheHighestValueFromObject,
count: vowelsCounter[keyWithTheHighestValueFromObject]
} // return the vowel and the count
console.log('Debigging')
}
const wordsArray = ['dog', 'cow', 'pig', 'chicken', 'horse']
console.log(JSON.stringify(mostFrequentVowel(wordsArray), null, 2))
/*
{
"vowel": "o",
"count": 3
}
*/