Last active
May 13, 2017 08:33
-
-
Save z4o4z/308bc4400ffa2edbd11308eb688105ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function zoom(n) { | |
const length = n % 2 ? n : n - 1; | |
const center = (length - 1) / 2; | |
if (length < 0) { | |
return ''; | |
} | |
function getSquare(square, level, symbol) { | |
if (level * 2 > length) { | |
return square.join('\n'); | |
} | |
const newSymbol = symbol === '□' ? '■' : '□'; | |
if (level === 0) { | |
square[center] = symbol; | |
return getSquare(square, level + 1, newSymbol); | |
} | |
const topAndBottomRow = new Array(level * 2 + 2).join(symbol); | |
const topRowIndex = center - level; | |
const bottomRowIndex = center + level; | |
square[topRowIndex] = topAndBottomRow; | |
square[bottomRowIndex] = topAndBottomRow; | |
Array.from({ length: bottomRowIndex - topRowIndex - 1 }, (_, i) => { | |
const index = topRowIndex + i + 1; | |
square[index] = `${symbol}${square[index]}${symbol}`; | |
}); | |
return getSquare(square, level + 1, newSymbol); | |
} | |
return getSquare(new Array(length), 0, '■'); | |
} | |
console.log(zoom(100)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment