Code Lab

Word in a frame

HardStrings15 min

Write a function frameWord(word, width, height) that puts word in the middle of a frame of stars and returns the whole frame as one string.

  • word is 1 to 20 Latin letters. width and height are whole numbers: the size of the frame in characters and in lines, the border included.
  • The first and the last line are width stars *. Every other line starts and ends with * and has spaces inside.
  • The word is in a middle line, in the middle of that line.
  • If the free space to the left and to the right of the word cannot be split evenly, the extra space goes to the right. Math.floor gives you the left part.
  • If the empty lines above and below the word cannot be split evenly, the extra line goes below. So the word is in the upper one of the two middle lines.
  • Join the lines with '\n'. There is no '\n' at the end.
  • If width is less than word.length + 2 or height is less than 3, the word does not fit: throw a RangeError.

A template literal is a short way to build one line of the frame.

Examples

console.log(frameWord('cat', 7, 5))

Prints ******* | * * | * cat * | * * | *******

frameWord('hi', 7, 3)

Returns '*******\n* hi *\n*******'

Source

Loading the editor...
  1. InputframeWord('cat', 7, 5)
    Expected'*******\n* *\n* cat *\n* *\n*******'
  2. InputframeWord('go', 4, 3)
    Expected'****\n*go*\n****'

Hidden tests run on Submit.