Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142239

returns [] when passed an empty string and returns the lengths when passed multiple words

$
0
0

so i need to write a function that gets the length of each individual words on the array that will pass these conditions

it("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]);
  });
  it("returns lengths for longer sentences", () => {
    expect(getWordLengths("like a bridge over troubled water")).to.eql([
      4,
      1,
      6,
      4,
      8,
      5
    ]);

my initial solution works but I want to use .map instead. So far I got

let x = str.split('');
console.log(x.map(nums => nums.length))

but that won't return [] when passed through an empty array


Viewing all articles
Browse latest Browse all 142239

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>