Skip to content

Latest commit

 

History

History
46 lines (33 loc) · 1022 Bytes

ascii-fun-1-x-shape.md

File metadata and controls

46 lines (33 loc) · 1022 Bytes

ASCII Fun #1: X-Shape 6 Kyu

LINK TO THE KATA - ASCII ART

Description

You will get an odd integer n (>= 3) and your task is to draw an X. Each line is separated with \n.

Use the following characters: ■ □

Examples

                                     ■□□□■
            ■□■                      □■□■□
  x(3) =>   □■□             x(5) =>  □□■□□
            ■□■                      □■□■□
                                     ■□□□■

Solution

const BLACK_BRICK = '□'
const WHITE_BRICK = '■'

const x = n => {
  let xShape = ''

  for (let i = 0; i < n; i++) {
    let row = BLACK_BRICK.repeat(n).split('')

    row[i] = WHITE_BRICK
    row[n - 1 - i] = WHITE_BRICK

    row = row.join('')
    xShape += `${row}\n`
  }

  return xShape.slice(0, -1)
}