Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //segitiga sama sisi (bil ganjil)
- {
- let ganjil = 1;
- const no1 = (n: number) => {
- for (let i = 1; i <= n; i++) {
- let row = " ";
- let str = ganjil + " ";
- let cetak = row.repeat(n - i) + str.repeat(i);
- console.log(cetak);
- ganjil += 2;
- }
- };
- no1(5);
- }
- //segitiga sama sisi terbalik (bil ganjil)
- {
- let ganjil = 1;
- const no2 = (n: number) => {
- for (let i = n; i >= 1; i--) {
- let start = " ";
- let space = start.repeat(n - i);
- let result = (ganjil + start).repeat(i);
- console.log(space, result);
- ganjil += 2;
- }
- };
- no2(5);
- }
- //segitiga siku-siku (abjad kecil)
- {
- let charCode = 97;
- const no3 = (n: number) => {
- for (let i = 1; i <= n; i++) {
- let result = String.fromCharCode(charCode).repeat(i);
- console.log(result);
- charCode++;
- }
- };
- no3(5);
- }
- //diagonal kiri atas ke kanan bawah (abjad besar)
- {
- const no4 = (n: number) => {
- let charcode = 65;
- for (let i = 0; i <= n; i++) {
- let space = " ";
- console.log(space.repeat(i) + String.fromCharCode(charcode));
- charcode++;
- }
- };
- no4(5);
- }
- //diagonal kanan atas ke kiri bawah (bil ganjil)
- {
- let ganjil = 1;
- const no5 = (n: number) => {
- for (let i = 0; i <= n; i++) {
- let space = " ";
- console.log(space.repeat(n - i) + ganjil);
- ganjil += 2;
- }
- };
- no5(5);
- }
- //diagonal kiri dan kanan (abjad kecil dan bilangan genap)
- {
- const no6 = (jumlah: number) => {
- let charCode = 97;
- let num = 2;
- let start = 1;
- let end = jumlah;
- let x = "";
- for (let i = 1; i <= jumlah; i++) {
- for (let j = 1; j <= jumlah; j++) {
- if (j == start) {
- x += String.fromCharCode(charCode);
- } else if (j == end) {
- x += num;
- } else {
- x += " ";
- }
- }
- x += "\n";
- charCode++;
- num++;
- start++;
- end--;
- }
- console.log(x);
- };
- no6(5);
- }
- //segitiga siku-siku terbalik (huruf abjad kecil)
- {
- let charCode = 97;
- const no7 = (n: number) => {
- for (let i = 0; i <= n; i++) {
- console.log(String.fromCharCode(charCode).repeat(n - i));
- charCode++;
- }
- };
- no7(5);
- }
- //segitiga siku-siku (bil genap)
- {
- let genap = 0;
- const no8 = (n: number) => {
- for (let i = 1; i <= n; i++) {
- let str = genap + " ";
- let cetak = str.repeat(i);
- console.log(cetak);
- genap += 2;
- }
- };
- no8(5);
- }
- //segitiga siku-siku terbalik (bil ganjil)
- {
- let ganjil = 1;
- const soal9 = (n: any) => {
- for (let i = n; i >= 1; i--) {
- let row = " " + ganjil;
- let cetak = row.repeat(i);
- console.log(cetak);
- ganjil += 2;
- }
- };
- soal9(5);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement