코드로 우주평화

Testing 1-2-3 본문

나는 이렇게 학습한다/Algorithm & SQL

Testing 1-2-3

daco2020 2022. 3. 10. 23:47
반응형

Description:

Your team is writing a fancy new text editor and you've been tasked with implementing the line numbering.

Write a function which takes a list of strings and returns each line prepended by the correct number.

The numbering starts at 1. The format is n: string. Notice the colon and space in between.

Examples:

number([]) // => []
number(["a", "b", "c"]) // => ["1: a", "2: b", "3: c"]

 

 

Solution:

1. Fetch the elements of an array.
2. Number the elements in order.
3. Concatenate the number and element into a string and return it as an array.

 

var number=function(array){
  let i = 1
  return array.map( str => `${i++}: ${str}`)
}

 

 

 

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Count the divisors of a number  (0) 2022.03.12
Maximum Length Difference  (0) 2022.03.11
Find the middle element  (0) 2022.03.09
Highest and Lowest  (0) 2022.03.08
Who likes it?  (0) 2022.03.07