Created
April 24, 2018 18:56
-
-
Save dagolinuxoid/38d0472013f9f8c0fc708e2a96558698 to your computer and use it in GitHub Desktop.
#beCareful #let #var #scope #kata
This file contains hidden or 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
https://www.codewars.com/kata/give-me-a-diamond/train/javascript | |
// doesn't work! | |
function diamond(n){ | |
if (n<=0 || n%2===0) return null; | |
function buildLine() { | |
let spaces = ' '.repeat((n-i)/2); | |
let stars = '*'.repeat(i); | |
return spaces+stars+'\n'; | |
} | |
const mid = '*'.repeat(n)+'\n'; | |
let before = '', after = ''; | |
for(let i=1; i<n; i+=2) before += buildLine(); | |
for(let i=n-2; i>0; i-=2) after += buildLine(); | |
return before + mid + after; | |
} | |
// it works | |
function diamond(n){ | |
if (n<=0 || n%2===0) return null; | |
function buildLine(num) { | |
let spaces = ' '.repeat((n-num)/2); | |
let stars = '*'.repeat(num); | |
return spaces+stars+'\n'; | |
} | |
const mid = '*'.repeat(n)+'\n'; | |
let before = '', after = ''; | |
for(let i=1; i<n; i+=2) before += buildLine(i); | |
for(let i=n-2; i>0; i-=2) after += buildLine(i); | |
return before + mid + after; | |
} | |
// it also does | |
function diamond(n){ | |
if (n<=0 || n%2===0) return null; | |
function buildLine() { | |
let spaces = ' '.repeat((n-i)/2); | |
let stars = '*'.repeat(i); | |
return spaces+stars+'\n'; | |
} | |
const mid = '*'.repeat(n)+'\n'; | |
let before = '', after = ''; | |
for(var i=1; i<n; i+=2) before += buildLine(); | |
for(i=n-2; i>0; i-=2) after += buildLine(); | |
return before + mid + after; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment