DEV Community

Discussion on: Daily Challenge #2 - String Diamond

Collapse
 
centanomics profile image
Cent

Just used some for loops to print all of the asterisks, after doing a check for over 0 and odd, and added some css to make the shape of a diamond.

Codepen


const stringDiamond = rows => {
  if (rows > 0 && rows % 2 === 1) {
    let diamond = "";
    for(let i = 1; i< rows + 1; i += 2) {
      diamond += "<p>";
      for(let j = 1; j < i + 1; j++) {
        diamond += "*";
      }
      diamond += "</p>";
    }
    for(let i = rows - 2; i> 0; i-=2) {
      diamond += "<p>";
      for(let j = 1; j < i + 1; j++) {
        diamond += "*";
      }
      diamond += "</p>";
    }
    return diamond;
  } else {
    return "null"
  }
}

The CSS making the asterisks look like a diamond


#answer {
  text-align: center;
  line-height: 5px;
}