noknow.dev
Sign inSign up
Course overview
JavaScript Fundamentals
0 / 7 lessons0%

Getting Started

  • Hello, World!
  • Variables: let and const
  • Numbers and Math

Decisions & Loops

  • if / else — Making Decisions
  • for Loops

Functions

  • Defining Functions
  • Arrow Functions

for Loops

0m 00s

Automating repetition

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 value
  • i < 5 → condition (keeps running while true)
  • i++ → increment i by 1 after each iteration

Example: calculating a sum

let sum = 0
for (let i = 1; i <= 10; i++) {
  sum = sum + i   // or: sum += i
}
console.log(sum)  // 55

Your task

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

Back
javascriptCtrl+Enter to run
Output

Click "Run" to execute your code.