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

09-17 08:12