Modo entrevista. Temporizador de 15 min, sin pistas y sin autocompletado
Word in a frame
DifícilStrings15 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.
Ejemplos
console.log(frameWord('cat', 7, 5))Imprime ******* | * * | * cat * | * * | *******
frameWord('hi', 7, 3)Devuelve '*******\n* hi *\n*******'
Cargando el editor…
- Entrada
frameWord('cat', 7, 5)Esperado'*******\n* *\n* cat *\n* *\n*******' - Entrada
frameWord('go', 4, 3)Esperado'****\n*go*\n****'
Las pruebas ocultas se ejecutan al enviar