Mode entretien. Minuteur de 15 min, sans indices ni autocomplétion
Word in a frame
DifficileStrings15 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.
Exemples
console.log(frameWord('cat', 7, 5))Affiche ******* | * * | * cat * | * * | *******
frameWord('hi', 7, 3)Renvoie '*******\n* hi *\n*******'
Chargement de l'éditeur...
- Entrée
frameWord('cat', 7, 5)Attendu'*******\n* *\n* cat *\n* *\n*******' - Entrée
frameWord('go', 4, 3)Attendu'****\n*go*\n****'
Les tests cachés se lancent à la soumission