Skip to content

Instantly share code, notes, and snippets.

@Gholamrezadar
Last active August 4, 2025 06:07
Show Gist options
  • Save Gholamrezadar/95d263c96721f98cdf9659e824a06b22 to your computer and use it in GitHub Desktop.
Save Gholamrezadar/95d263c96721f98cdf9659e824a06b22 to your computer and use it in GitHub Desktop.
for_loop_exercises

For Loop Practice

Single For Loop

1. Simple

Description: Print the numbers from 0 to 9.

0
1
2
3
4
5
6
7
8
9
Solution
// The most basic for loop
for (let i = 0; i < 10; i++) {
    console.log(i);
}

2. Simple on a Single Line

Description: Print the numbers from 0 to 9 on a single line.

0123456789
Solution
// store everything in a single line and console log it once
let line = "";
for (let i = 0; i < 10; i++) {
    line += i; 
}
console.log(line);

2. 10 EACH

n = 10

00000000001111111111 // 10 0s and 10 1s
Solution
let n = 10;
let line = "";

// One for loop for the 0s
for (let i = 0; i < n; i++) {
    line += "0";
}

// One for loop for the 1s
for (let i = 1; i <= n; i++) {
    line += "1";
}

// console log the whole line
console.log(line);

Can you solve this with only one for loop?

3. Alternating

n = 10 (Use only ONE for loop)

01010101010101010101 // 10 1s and 10 0s alternating
Help You can use the modulus operator (%) to check if a number is even or odd.
if (i % 2 === 0) {
    // i is even, do something
} else {
    // i is odd, do something else
}
Cheat Solution
let line = "";
let n = 10;
for (let i = 0; i < n; i++) {
        line += "01";
}
console.log(line);
Good Solution
let line = "";
let n = 10;
for (let i = 0; i < 2*n; i++) {
    if (i % 2 === 0) {
        line += "0";
    } else {
        line += "1";
    }
}
console.log(line);

Nested For Loops

1. Classic Stars

Description: Given a positive integer n, print a classic n-star pattern.

n = 5

*
**
***
****
*****
Solution
for (let i = 1; i <= n; i++) {
  let line = "";
  for (let j = 1; j <= i; j++) {
    line += "*";
  }
  console.log(line);
}

2. Reversed Stars

n = 5

*****
****
***
**
*
Solution (-10 Aura)
for (let i = n; i >= 1; i--) {
  let line = "";
  for (let j = 1; j <= i; j++) {
    line += "*";
  }
  console.log(line);
}

3. Right-Aligned Triangle

n = 5

    *
   **
  ***
 ****
*****
Solution (-20 Aura)
for (let i = 1; i <= n; i++) {
  let line = "";
  for (let space = 1; space <= n - i; space++) {
    line += " ";
  }
  for (let star = 1; star <= i; star++) {
    line += "*";
  }
  console.log(line);
}

4. Pyramid Pattern

n = 5

    *
   ***
  *****
 *******
*********
Solution
for (let i = 1; i <= n; i++) {
  let line = "";
  for (let space = 1; space <= n - i; space++) {
    line += " ";
  }
  for (let star = 1; star <= 2 * i - 1; star++) {
    line += "*";
  }
  console.log(line);
}

5. Inverted Pyramid

n = 5

*********
 *******
  *****
   ***
    *
Solution (-20 Aura)
for (let i = n; i >= 1; i--) {
  let line = "";
  for (let space = 1; space <= n - i; space++) {
    line += " ";
  }
  for (let star = 1; star <= 2 * i - 1; star++) {
    line += "*";
  }
  console.log(line);
}

6. Number Triangle

n = 5

1
12
123
1234
12345
Solution (-10 Aura)
for (let i = 1; i <= n; i++) {
  let line = "";
  for (let j = 1; j <= i; j++) {
    line += j;
  }
  console.log(line);
}

7. Repeated Numbers

n = 5

1
22
333
4444
55555
Solution (-20 Aura)
for (let i = 1; i <= n; i++) {
  let line = "";
  for (let j = 1; j <= i; j++) {
    line += i;
  }
  console.log(line);
}

8. Repeated Numbers Reversed

n = 5

5
44
333
2222
11111

9. Repeated Numbers interleaved

n = 5

1
33
222
5555
44444

10. Power

n = 5

#
##
####
########
################
################################

Final Projects

1. Diamond

n = 5

    *
   ***
  *****
   ***
    *
Solution
No Solution!

2. Checkerboard

###   ###   ###
   ###   ###
###   ###   ###
   ###   ###
###   ###   ###
   ###   ###

and

// Should look square in the console

####    ####    ####
####    ####    ####
    ####    ####    ####
    ####    ####    ####
####    ####    ####
####    ####    ####
    ####    ####    ####
    ####    ####    ####
Solution
No Solution!
Help
Shift+3 = #

Good Luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment