Skip to content

Instantly share code, notes, and snippets.

@dagolinuxoid
Created April 24, 2018 18:56
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 dagolinuxoid/38d0472013f9f8c0fc708e2a96558698 to your computer and use it in GitHub Desktop.
Save dagolinuxoid/38d0472013f9f8c0fc708e2a96558698 to your computer and use it in GitHub Desktop.
#beCareful #let #var #scope #kata
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