Advertisement
Spocoman

10. Diamond

Jan 4th, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function diamond(number) {
  2.     let num = parseInt(number);
  3.     for (let i = 0; i < num / 2; i++) {
  4.         if (i === 0) {
  5.             console.log(`${'-'.repeat(((num - 1) / 2) - i)}${num % 2 === 0 ? '**' : '*'}${'-'.repeat((num - 1) / 2 - i)}`);
  6.         } else {
  7.             console.log(`${'-'.repeat(((num - 1) / 2) - i)}*${'-'.repeat(i * 2 - num % 2)}*${'-'.repeat((num - 1) / 2 - i)}`);
  8.         }
  9.     }
  10.  
  11.     for (let i = (num - 2 + (num % 2)) / 2 - 1; i >= 0; i--) {
  12.  
  13.         if (i === 0) {
  14.             console.log(`${'-'.repeat(((num - 1) / 2) - i)}${num % 2 === 0 ? '**' : '*'}${'-'.repeat((num - 1) / 2 - i)}`);
  15.         } else {
  16.             console.log(`${'-'.repeat(((num - 1) / 2) - i)}*${'-'.repeat(i * 2 - num % 2)}*${'-'.repeat((num - 1) / 2 - i)}`);
  17.         }
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement