본문 바로가기
알고리즘/Codewars

[Codewars] Add Length (8 kyu) / JavaScript

by fluss 2023. 4. 3.

https://www.codewars.com/kata/559d2284b5bb6799e9000047

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

What if we need the length of the words separated by a space to be added at the end of that same word and have it returned as an array?

Example(Input --> Output)

"apple ban" --> ["apple 5", "ban 3"]
"you will win" -->["you 3", "will 4", "win 3"]

Your task is to write a function that takes a String and returns an Array/list with the length of each word added to each element .

Note: String will have at least one element; words will always be separated by a space.

 

코드

function addLength(str) {
  return str.split(' ').map(el => el + ' ' + el.length);
}

댓글