Skip to content

Instantly share code, notes, and snippets.

@z4o4z
Last active May 13, 2017 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save z4o4z/308bc4400ffa2edbd11308eb688105ba to your computer and use it in GitHub Desktop.
Save z4o4z/308bc4400ffa2edbd11308eb688105ba to your computer and use it in GitHub Desktop.
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