javascript pyramid loop

halo suhu, ada yang bisa bantu saya buat/membetulkan coding untuk output:

                                                               1

                                              2                             2

                                 3                             4                             3

                4                             5                             5                             4

5                             6                             7                             6                             5

saya coba pakai:

for (let i = 1; i <= 5; i++) {

let output = "";

for (let j = 1; j <= 5; j++) {

    if (j < 6 - i) {

      output += " ";

    } else {

      output += `${j - (5 - i)} `;

    }

}

console.log(output);

}

hasilnya begini:

    1
   1 2
  1 2 3
 1 2 3 4
1 2 3 4 5

mohon bantuannya suhu, ane pemula. terima kasih :)

avatar Nugrahar
@Nugrahar

3 Kontribusi 1 Poin

Diperbarui 1 tahun yang lalu

2 Jawaban:

Jawaban Terpilih

<div>Bisa dicoba logika code dibawah jadi loop (for..)&nbsp;</div><div class="attachment-gallery attachment-gallery--3"><figure data-trix-attachment="{&quot;contentType&quot;:&quot;image/png&quot;,&quot;filename&quot;:&quot;sk_nugrahar_pyramid_loop_v5.png&quot;,&quot;filesize&quot;:374130,&quot;height&quot;:1272,&quot;url&quot;:&quot;https://i.ibb.co/n1CrBtH/sk-nugrahar-pyramid-loop-v5.png&quot;,&quot;width&quot;:4140}" data-trix-content-type="image/png" data-trix-attributes="{&quot;caption&quot;:&quot;Pyramid Loop&quot;,&quot;presentation&quot;:&quot;gallery&quot;}" class="attachment attachment--preview attachment--png"><img src="https://i.ibb.co/n1CrBtH/sk-nugrahar-pyramid-loop-v5.png" width="4140" height="1272"><figcaption class="attachment__caption attachment__caption--edited">Pyramid Loop</figcaption></figure><figure data-trix-attachment="{&quot;contentType&quot;:&quot;image/png&quot;,&quot;filename&quot;:&quot;sk_nugrahar_pyramid_loop_v7.png&quot;,&quot;filesize&quot;:312828,&quot;height&quot;:1668,&quot;url&quot;:&quot;https://i.ibb.co/59V4XMv/sk-nugrahar-pyramid-loop-v7.png&quot;,&quot;width&quot;:2848}" data-trix-content-type="image/png" data-trix-attributes="{&quot;caption&quot;:&quot;Pyramid Loop via For&quot;,&quot;presentation&quot;:&quot;gallery&quot;}" class="attachment attachment--preview attachment--png"><img src="https://i.ibb.co/59V4XMv/sk-nugrahar-pyramid-loop-v7.png" width="2848" height="1668"><figcaption class="attachment__caption attachment__caption--edited">Pyramid Loop via For</figcaption></figure><figure data-trix-attachment="{&quot;contentType&quot;:&quot;image/png&quot;,&quot;filename&quot;:&quot;sk_nugrahar_pyramid_loop_result.png&quot;,&quot;filesize&quot;:2974,&quot;height&quot;:269,&quot;url&quot;:&quot;https://i.ibb.co/Qjt187B/sk-nugrahar-pyramid-loop-result.png&quot;,&quot;width&quot;:232}" data-trix-content-type="image/png" data-trix-attributes="{&quot;caption&quot;:&quot;Result&quot;,&quot;presentation&quot;:&quot;gallery&quot;}" class="attachment attachment--preview attachment--png"><img src="https://i.ibb.co/Qjt187B/sk-nugrahar-pyramid-loop-result.png" width="232" height="269"><figcaption class="attachment__caption attachment__caption--edited">Result</figcaption></figure></div><div><figure data-trix-attachment="{&quot;contentType&quot;:&quot;image/png&quot;,&quot;filename&quot;:&quot;sk_nugrahar_pyramid_loop_test.png&quot;,&quot;filesize&quot;:81142,&quot;height&quot;:1788,&quot;url&quot;:&quot;https://i.ibb.co/ynsqV37/sk-nugrahar-pyramid-loop-test.png&quot;,&quot;width&quot;:1384}" data-trix-content-type="image/png" data-trix-attributes="{&quot;caption&quot;:&quot;Pyramid Loop Test&quot;,&quot;presentation&quot;:&quot;gallery&quot;}" class="attachment attachment--preview attachment--png"><img src="https://i.ibb.co/ynsqV37/sk-nugrahar-pyramid-loop-test.png" width="1384" height="1788"><figcaption class="attachment__caption attachment__caption--edited">Pyramid Loop Test</figcaption></figure><br><br></div>

avatar soeleman
@soeleman

120 Kontribusi 147 Poin

Dipost 1 tahun yang lalu

Tanggapan

kok di ane malah true false true false ya hu?

mungkin ternary operator (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) jadi satu line dan kalau copy code-nya coba copy dengan detail -- tanda kurung dan sebagainya.

mantap hu :D. ane tambah dengan sedikit bumbu versi ane sendiri hehehe. thank you suhu..

<div>@soeleman maap hu ane bingung mengubah logikanya😩. apa bisa minta tolong langsung buat fornya hu? &nbsp;<br><br>tapi sdh ada perbaikan codingnya: (walau belum ketemu)&nbsp;<br>for (let i = 1; i &lt;= 5; i++) {<br><br>&nbsp; let output = " ";<br><br>&nbsp; for (let j = 1; j &lt;= 5; j++) {<br><br>&nbsp; &nbsp; if ( j &lt; 6 - i) {<br><br>&nbsp; &nbsp; &nbsp; output += " ";<br><br>&nbsp; &nbsp; } else {<br><br>&nbsp; &nbsp; &nbsp; output += ${j -&amp;nbsp; (j - i)} ;<br><br>&nbsp; &nbsp; }<br><br>&nbsp; }<br><br>&nbsp; console.log(output);<br><br>}&nbsp;</div>

avatar Nugrahar
@Nugrahar

3 Kontribusi 1 Poin

Dipost 1 tahun yang lalu

Login untuk ikut Jawaban