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.
wordis 1 to 20 Latin letters.widthandheightare whole numbers: the size of the frame in characters and in lines, the border included.- The first and the last line are
widthstars*. 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
widthis less thanword.length + 2orheightis less than 3, the word does not fit: throw aRangeError.
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*******'
Loading the editor...
- Input
frameWord('cat', 7, 5)Expected'*******\n* *\n* cat *\n* *\n*******' - Input
frameWord('go', 4, 3)Expected'****\n*go*\n****'
Hidden tests run on Submit