반응형
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 |