DEV Community

Discussion on: Daily Challenge #2 - String Diamond

Collapse
 
kvharish profile image
K.V.Harish

My solution in js


const padSpaceToSides = (str, len) => {
        const padlen = len - str.length,
          right = Math.ceil( padlen / 2 ),
          left = padlen - right;
        return str = Array(left+1).join(' ') + str + Array(right+1).join(' ');
      },
      diamondTopFormation = (end, atom) => {
        const start = 1, step = 2;
        for(let i = start; i < end; i += step) {
          console.log(padSpaceToSides(atom.repeat(i), end));
        }
      },
      diamondBottomFormation = (start, atom) => {
        const end = 1, step = 2;
        for(let i = start; i >= end; i -= step) {
          console.log(padSpaceToSides(atom.repeat(i), start));
        }
      },
      diamond = (height, atom = '*') => {
        if(height % 2 === 0 || height < 0) {
          return null;
        }
        diamondTopFormation(height, atom);
        diamondBottomFormation(height, atom);
      };

diamond(5);