面试题答案
一键面试function analyzeString(str) {
// 去除两端空格并按空格分割字符串
const words = str.trim().split(' ');
let wordCount = words.length;
let longestWord = '';
let totalLength = 0;
words.forEach(word => {
if (word.length > longestWord.length) {
longestWord = word;
}
totalLength += word.length;
});
let averageWordLength = wordCount === 0? 0 : Math.round(totalLength / wordCount * 10) / 10;
return {
wordCount,
longestWord,
averageWordLength
};
}