When you want to run code multiple times, use a loop. The for loop has three parts:
for (let i = 0; i < 5; i++) {
console.log(i)
}
// Output: 0, 1, 2, 3, 4
let i = 0 → starting valuei < 5 → condition (keeps running while true)i++ → increment i by 1 after each iterationlet sum = 0
for (let i = 1; i <= 10; i++) {
sum = sum + i // or: sum += i
}
console.log(sum) // 55
Write a function sumUpTo(n) that adds all numbers from 1 to n and returns the result.
Example: sumUpTo(5) → 1 + 2 + 3 + 4 + 5 = 15
Click "Run" to execute your code.