DEV Community

Discussion on: Daily Challenge #2 - String Diamond

Collapse
dimitrilahaye profile image
Dimitri Lahaye

Sorry for that (JS)

function diamond(int) {
  if (int < 0 || !(int % 2)) {
    return null;
  }
  write = (i,j) => ' '.repeat(j) + '*'.repeat(i) + '\n';
  let ret = '';
  for (let i = 1, j = Math.floor(int / 2); j >= 0; j--, i += 2) {
    ret += write(i,j);
  }
  for (let i = int - 2, j = 1; j < int && i > 0; j++, i -= 2) {
    ret += write(i,j);
  }
  return ret;
}