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

Time:01-11

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' ]

CodePudding user response:

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

  • Related