2

I'm new to coding and have been given this question to solve.

The question I have been given is this;

Create a function that takes a string and returns an array of the lengths of each word in the string.

 E.g. 'pineapple and black bean curry' => [9, 3, 5, 4, 5]

The code that I have written is this;

function getWordLengths(str) {
    let len = []
    let words = str.split()
    for (let i = 0; i < words.length; i++){
        len.push(words[i])}
    
    return len
}

My code will be run against this test;

describe("getWordLengths", () => {
 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
    ]);
 });
});

Dose anyone have any suggestion of haw to make my code work?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Geo
  • 2,321
  • 2
  • 12
  • 19

5 Answers5

2

You can use string#split and then use map to get the length of each word.

let string = 'pineapple and black bean curry',
    result = string.split(/\s+/).map(({length}) => length);
console.log(result)
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
1

You are not passing any separator to your split function.

So, split the string at every " ", then calculate the length of each word from the resulting array using map:

let string = 'pineapple and black bean curry';

console.log(string.split(' ').map(a => a.length))
adiga
  • 34,372
  • 9
  • 61
  • 83
1

I changed .split() to .split(' ') and len.push(words[i]) to len.push(words[i].length).

const text = 'pineapple and black bean curry'; //[9, 3, 5, 4, 5]

function getWordLengths(str) {
  let len = [];
  let words = str.split(' ');
  for (let i = 0; i < words.length; i++){
    len.push(words[i].length);
  }
  return len;
}

console.log(getWordLengths(text));
holydragon
  • 6,158
  • 6
  • 39
  • 62
0

\b in a regex expression to split words.

let line='pineapple and black bean curry';
let results=line.match(/\b[^\s]+?\b/g).map(r=>r.length);
console.log(results)
lx1412
  • 1,160
  • 6
  • 14
0

There are quite a few ways to do this buddy.

  1. The simplest way would probably be to do as you attempted, i.e., to use the .split() feature. It works only when used rightly. You have forgotten to split the string based on a separator. And here since you are interested in individual words, the separator should be a space. ' '. just map the lengths of the words onto a new array and print it out.

let string = 'pineapple and black bean curry';
let words = string.split(' ');
console.log(words.map(word => word.length))
  1. If you don't want to use map or split functionalities and want to do it using straightforward manual ways, you can do the following: In this , you just keep a counter variable which you increment as long as you don't encounter a space or end of the string and then reset it at the start of the next word.

let string = 'pineapple and black bean curry';
let count = 0, count_array = [];
let x = string.length;

for(i=0;i<=x;i++){
if(string[i]==' ' || string[i]=='\0' || i==x){
count_array.push(count);
count=0;
}
else{
count++;
}
}

console.log(count_array);
Harshith Rai
  • 3,018
  • 7
  • 22
  • 35