Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function romboidHalfingTheRows(input) {
- // in case the pyramid is even numver rows, the diamond will be rows-1 rows, because, otherwise it can't get appropriate center point to turn into diamond.
- let rows = input[0];
- let stars = '';
- let spaces = '';
- let secondSpaces = '';
- let seconsStars = '';
- let oddCasesString = '';
- for (let i = 1; i <= rows; i++) { //Making the first half which is normal pyramid and it's rows are half of the all rolls
- if (i <= rows / 2) { //defining when the normal pyramid needs to stop printing
- if (i === 1) {
- stars += '*'
- } else {
- stars += '**'
- }
- for (let j = 1; j <= rows - i; j++) {
- spaces += ' '
- }
- console.log(spaces + stars)
- spaces = '';
- }
- }
- if (rows % 2 !== 0) { //Condition to make the pyrmid correct if there are odd numbers of rows
- //if you don't do that step, and the rows are odd, the center two rows will be with equal stars
- // and it wont form diamond shape
- let counterSpaces = Math.trunc(rows * 0.5)
- let counterStars = rows;
- for (let m = 1; m <= counterSpaces; m++) {
- oddCasesString += ' ';
- }
- for (let l = 1; l <= counterStars; l++) {
- oddCasesString += '*'
- }
- console.log(oddCasesString)
- }
- for (let i = 1; i <= rows - 1; i++) { // making the upsidedown pyramid and its rows are the other half of the all rows
- if (i > rows / 2) { //defining when to start printing it.
- for (let j = rows - i; j >= 1; j--) {
- if (j === 1) {
- seconsStars += '*'
- } else {
- seconsStars += '**'
- }
- }
- for (let n = rows - i; n <= rows - 1; n++) {
- secondSpaces += ' '
- }
- console.log(secondSpaces + seconsStars)
- seconsStars = '';
- secondSpaces = '';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment