gpt4 book ai didi

javascript - 代码错误 - 分割字符串并返回每个单词的长度 JS

转载 作者:行者123 更新时间:2023-11-28 03:19:32 25 4
gpt4 key购买 nike

function getWordLengths(str) {

return str.split(' ').map(words => words.length)
}

我的错误是

AssertionError: expected [ 0 ] to deeply equal []
+ expected - actual

-[
- 0
-]
+[]

t('returns [] when passed an empty string', () => {
expect(getWordLengths('')).to.eql([]);
});
it('returns an array containing the length of a single word', () => {
expect(getWordLengths('woooo')).to.eql([5]);
});
it('returns the lengths when passed multiple words', () => {
expect(getWordLengths('hello world')).to.eql([5, 5]);
});

最佳答案

你可以使用这样的东西:

function getWordLengths(str) {
return str.length > 0 ? str.split(' ').map(words => words.length) : [];
}

console.log( getWordLengths(""));
console.log( getWordLengths("Hi") );
console.log( getWordLengths("Hi there how are you") );

关于javascript - 代码错误 - 分割字符串并返回每个单词的长度 JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59292333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com