Function that takes a string and returns an array with the length of each word added to each element

Advertisements

I tried split() to make an array out of it then map() to add numbers next to the strings.

function nameLength(str) {
    const words = str.split(" ");
    return words.map(str => $(words) $(words.length));
}

console.log(nameLength("hawaii pizza"));

was expecting:

[ 'hawaii 6', 'pizza 5' ]

>Solution :

You are returning the wrong string. also your syntax is also wrong
just try this

function nameLength(str) {
    const words = str.split(" ");
    return words.map(str => `${str} ${str.length}`);
}

console.log(nameLength("hawaii pizza"));

Leave a Reply Cancel reply