Advertisement
alvinfnaldi

Typescript Loop

Sep 21st, 2023
1,000
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //segitiga sama sisi (bil ganjil)
  2. {
  3.   let ganjil = 1;
  4.  
  5.   const no1 = (n: number) => {
  6.     for (let i = 1; i <= n; i++) {
  7.       let row = " ";
  8.       let str = ganjil + " ";
  9.       let cetak = row.repeat(n - i) + str.repeat(i);
  10.       console.log(cetak);
  11.       ganjil += 2;
  12.     }
  13.   };
  14.   no1(5);
  15. }
  16.  
  17. //segitiga sama sisi terbalik (bil ganjil)
  18. {
  19.   let ganjil = 1;
  20.  
  21.   const no2 = (n: number) => {
  22.     for (let i = n; i >= 1; i--) {
  23.       let start = " ";
  24.       let space = start.repeat(n - i);
  25.       let result = (ganjil + start).repeat(i);
  26.       console.log(space, result);
  27.       ganjil += 2;
  28.     }
  29.   };
  30.   no2(5);
  31. }
  32.  
  33. //segitiga siku-siku (abjad kecil)
  34. {
  35.   let charCode = 97;
  36.   const no3 = (n: number) => {
  37.     for (let i = 1; i <= n; i++) {
  38.       let result = String.fromCharCode(charCode).repeat(i);
  39.       console.log(result);
  40.       charCode++;
  41.     }
  42.   };
  43.   no3(5);
  44. }
  45.  
  46. //diagonal kiri atas ke kanan bawah (abjad besar)
  47. {
  48.   const no4 = (n: number) => {
  49.     let charcode = 65;
  50.  
  51.     for (let i = 0; i <= n; i++) {
  52.       let space = " ";
  53.       console.log(space.repeat(i) + String.fromCharCode(charcode));
  54.       charcode++;
  55.     }
  56.   };
  57.  
  58.   no4(5);
  59. }
  60.  
  61. //diagonal kanan atas ke kiri bawah (bil ganjil)
  62. {
  63.   let ganjil = 1;
  64.  
  65.   const no5 = (n: number) => {
  66.     for (let i = 0; i <= n; i++) {
  67.       let space = " ";
  68.       console.log(space.repeat(n - i) + ganjil);
  69.       ganjil += 2;
  70.     }
  71.   };
  72.   no5(5);
  73. }
  74.  
  75. //diagonal kiri dan kanan (abjad kecil dan bilangan genap)
  76. {
  77.   const no6 = (jumlah: number) => {
  78.     let charCode = 97;
  79.     let num = 2;
  80.     let start = 1;
  81.     let end = jumlah;
  82.     let x = "";
  83.     for (let i = 1; i <= jumlah; i++) {
  84.       for (let j = 1; j <= jumlah; j++) {
  85.         if (j == start) {
  86.           x += String.fromCharCode(charCode);
  87.         } else if (j == end) {
  88.           x += num;
  89.         } else {
  90.           x += " ";
  91.         }
  92.       }
  93.       x += "\n";
  94.       charCode++;
  95.       num++;
  96.       start++;
  97.       end--;
  98.     }
  99.     console.log(x);
  100.   };
  101.   no6(5);
  102. }
  103.  
  104. //segitiga siku-siku terbalik (huruf abjad kecil)
  105. {
  106.   let charCode = 97;
  107.  
  108.   const no7 = (n: number) => {
  109.     for (let i = 0; i <= n; i++) {
  110.       console.log(String.fromCharCode(charCode).repeat(n - i));
  111.       charCode++;
  112.     }
  113.   };
  114.  
  115.   no7(5);
  116. }
  117.  
  118. //segitiga siku-siku (bil genap)
  119. {
  120.   let genap = 0;
  121.  
  122.   const no8 = (n: number) => {
  123.     for (let i = 1; i <= n; i++) {
  124.       let str = genap + " ";
  125.       let cetak = str.repeat(i);
  126.       console.log(cetak);
  127.       genap += 2;
  128.     }
  129.   };
  130.   no8(5);
  131. }
  132.  
  133. //segitiga siku-siku terbalik (bil ganjil)
  134. {
  135.   let ganjil = 1;
  136.  
  137.   const soal9 = (n: any) => {
  138.     for (let i = n; i >= 1; i--) {
  139.       let row = " " + ganjil;
  140.       let cetak = row.repeat(i);
  141.       console.log(cetak);
  142.       ganjil += 2;
  143.     }
  144.   };
  145.  
  146.   soal9(5);
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement