Postingan lainnya
Kelas Premium!
Belajar Javascript untuk website
Gunakan kupon "mulaijs" untuk diskon 75 ribu!
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 :)
2 Jawaban:
Jawaban Terpilih
<div>Bisa dicoba logika code dibawah jadi loop (for..) </div><div class="attachment-gallery attachment-gallery--3"><figure data-trix-attachment="{"contentType":"image/png","filename":"sk_nugrahar_pyramid_loop_v5.png","filesize":374130,"height":1272,"url":"https://i.ibb.co/n1CrBtH/sk-nugrahar-pyramid-loop-v5.png","width":4140}" data-trix-content-type="image/png" data-trix-attributes="{"caption":"Pyramid Loop","presentation":"gallery"}" 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="{"contentType":"image/png","filename":"sk_nugrahar_pyramid_loop_v7.png","filesize":312828,"height":1668,"url":"https://i.ibb.co/59V4XMv/sk-nugrahar-pyramid-loop-v7.png","width":2848}" data-trix-content-type="image/png" data-trix-attributes="{"caption":"Pyramid Loop via For","presentation":"gallery"}" 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="{"contentType":"image/png","filename":"sk_nugrahar_pyramid_loop_result.png","filesize":2974,"height":269,"url":"https://i.ibb.co/Qjt187B/sk-nugrahar-pyramid-loop-result.png","width":232}" data-trix-content-type="image/png" data-trix-attributes="{"caption":"Result","presentation":"gallery"}" 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="{"contentType":"image/png","filename":"sk_nugrahar_pyramid_loop_test.png","filesize":81142,"height":1788,"url":"https://i.ibb.co/ynsqV37/sk-nugrahar-pyramid-loop-test.png","width":1384}" data-trix-content-type="image/png" data-trix-attributes="{"caption":"Pyramid Loop Test","presentation":"gallery"}" 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>
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? <br><br>tapi sdh ada perbaikan codingnya: (walau belum ketemu) <br>for (let i = 1; i <= 5; i++) {<br><br> let output = " ";<br><br> for (let j = 1; j <= 5; j++) {<br><br> if ( j < 6 - i) {<br><br> output += " ";<br><br> } else {<br><br> output += ${j -&nbsp; (j - i)}
;<br><br> }<br><br> }<br><br> console.log(output);<br><br>} </div>