The right loop: 12 levels of for, while, forEach, map, break and continue. A JavaScript looping game for picking the best iteration pattern.
A for loop repeats code a set number of times. Its shape: for (start; condition; increment). The loop runs while its condition is true.
A while loop repeats code while a condition is true. Make sure the condition changes inside the loop so it doesn't become an infinite loop.
for...of is the modern way to iterate array elements one by one. Cleaner than a plain for because you get the value directly.
break forcibly stops the loop. Useful when you've found what you're looking for and don't need to keep looping.
continue skips the rest of the code in the current iteration and jumps straight to the next. Useful for "skipping" certain items.
A loop can be placed inside another loop (nested). The outer loop runs once, the inner loop runs fully for each iteration of the outer loop.
hitungSampai that takes a number n and returns a string of the numbers 1 to n separated by commas. Example: hitungSampai(3) → "1,2,3".jumlahSampai that takes a number n and returns the sum 1 + 2 + ... + n.hitungMundur that takes a number n and returns a countdown string separated by commas. Example: hitungMundur(3) → "3,2,1".lipatGanda that takes a number target. Start from 1, keep multiplying by 2 until >= target. Return the result.gabungMenu that takes an array items and returns all the items joined with " & ". Example: gabungMenu(["Nasi", "Ayam"]) → "Nasi & Ayam".stempelHarga that takes an array of numbers harga and returns an array of strings in the format "Rp X". Example: stempelHarga([1000, 2000]) → ["Rp 1000", "Rp 2000"].cariPosisi that takes an array items and a string target. Return the first index (position) where the target is found. If it's not there, return -1.hanyaGenap that takes an array of numbers and returns an array of only the even numbers. Use a loop with continue to skip the odd numbers.tabelPerkalian that takes a number n and returns an array of the products 1×1 to n×n in order. Example: tabelPerkalian(2) → [1,2,2,4].polaBintang (star pattern) that takes a number n and returns an array of strings. Row i holds i stars *. Example: polaBintang(3) → ["*","","*"].fizzBuzz that takes a number n and returns an array: for each number 1..n, if divisible by 3 and 5 → "FizzBuzz", divisible by 3 → "Fizz", divisible by 5 → "Buzz", otherwise the number itself (as a string).cetakBatch that takes an array pesanan (numbers) and a number maxPerBatch. Group the orders into batches: each batch holds consecutive orders until the batch total reaches/exceeds maxPerBatch, then start a new batch. Return the number of batches. Example: cetakBatch([3,2,4,1,5], 5) → 3 (batches: [3,2], [4,1], [5]).