Skip to content

Instantly share code, notes, and snippets.

@elxris
Last active February 12, 2016 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elxris/baad6b493da22999f189 to your computer and use it in GitHub Desktop.
Save elxris/baad6b493da22999f189 to your computer and use it in GitHub Desktop.
Codefight NewYear Challenge

Challenge Url

After finishing your last CodeFight of 2015 you suddenly realize: the New Year is almost here! To share your excitement, you want to send a New Year's wishp to the community.

You think your message should look nice, so you decide to put it into a frame of asterisks ('*'), with each word on a separate line.

Write a function that returns a framed wish as an array of strings, where each string is a word of the wish (including any punctuation marks that might come right after it) with some spaces added to make the frame rectangular. Note that there should be exactly 1 whitespace character (' ') before each word, and at least 1 after.

Example

For wish = 'Happy New Year and CodeFight On in 2016!' the output should be

happyNewYear(wish) = ['*************', 
                      '* Happy     *', 
                      '* New       *', 
                      '* Year      *', 
                      '* and       *', 
                      '* CodeFight *', 
                      '* On        *', 
                      '* in        *', 
                      '* 2016!     *', 
                      '*************']
  • [input] string wish
    • Your wish that has at least one word.
  • [output] array.string
    • The framed version of your wish.
happyNewYear = w => {
w = w.split(/ /)
for(n=0;w.some(v => v[n]);)n++
return [b = '*'.repeat(n+4), ...w.map(v=> `* ${(v+' ').slice(0, n)} *`), b]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment