Last active
June 26, 2019 21:50
-
-
Save lubien/8412f87fdf6756a25ee8e40418211125 to your computer and use it in GitHub Desktop.
['a', 'b', 'c'] => "a, b e c" https://repl.it/@lubien/allowedExtensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function allowedExtensions (exts) { | |
return exts.map((item, i) => { | |
const first = i === 0 | |
const last = i === (exts.length - 1) | |
if (first) { | |
return [item] | |
} | |
if (last) { | |
return [' e ', item] | |
} | |
return [', ', item] | |
}) | |
// flatten | |
.reduce((acc, x) => acc.concat(x), []) | |
.join('') | |
} | |
console.log(allowedExtensions(['erick', 'azul', 'amarelo', 'estranhuS'])) | |
console.log(allowedExtensions(['feIo', 'estranhuS'])) | |
console.log(allowedExtensions(['estranhuS'])) | |
console.log(allowedExtensions([])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please come home
Array#flatMap