Diamond
Algorithm to create diamonds (taking advantage of new repeat function for shorter code)
by Marcus Baptiste
JavaScript
function diamond(n) {
if (n < 0 || n % 2 === 0) return null;
var d = '';
var len = 0;
var indent = 0;
for (let i=1; i<n*2; i+=2) {
len = n - Math.abs(n-i);
indent = (n - len) / 2;
d += ' '.repeat(indent) + '*'.repeat(len) + '\n';
}
return d;
}
function oneLineDiamond(n) {
//ABSOLUTE MADNESS
return (n<0 || n%2===0) ? null : '-'.repeat(n*2).split('').map((s,i)=> ' '.repeat((n-(n-Math.abs(n-i)))/2) + '*'.repeat(n-Math.abs(n-i)) + '\n').filter((s,i)=>i%2===1).join('');
}
console.log(diamond(11));
console.log('with one liner');
console.log(oneLineDiamond(5));