Skip to content

Instantly share code, notes, and snippets.

@MisterBrash
Last active April 24, 2024 21:55
Show Gist options
  • Save MisterBrash/a1b02145dcee9c160f9c55fa2ff91c88 to your computer and use it in GitHub Desktop.
Save MisterBrash/a1b02145dcee9c160f9c55fa2ff91c88 to your computer and use it in GitHub Desktop.

3.5 - Nested Loops

Recall: You can put if-statements inside other if-statements (infinitely)... that's called "nesting".

if (something == true) {
  if (some_other_thing == true) {
    if (x % 2 == 0) {
      // Code here
    }
  }
}

❓What happens if you place a loop inside another loop?


We can "nest" a loop inside another (as many times as we need) just like the if-statements.

for (let outter_loop = 0; outter_loop < 10; outter_loop++) {
  // Maybe some code here
  
  for (let inner_loop = 9; inner_loop > 0; inner_loop--) {
    // Code goes here
  }
  
  // Maybe other code here
}

They can be different types of loops, too! We can nest as many as we need to. Sometimes nesting three or four deep is necessary.


Discuss with a classmate - what will the following code print?

let output = "";
for (let x = 1; x < 8; x++) {
  let y = x;
  while (y > 0) {
    output += "*";
    y--;
  }
  console.log(output);
}

The absolute best way to learn this is through trying it. At this point you should know the syntax of loops and the reasons for using them. If you are at all confused, you probably missed the previous lessons on While, Do While, and For. YouTube has fantastic examples and tutorials.

Your Tasks:

Create a new coding file for yourself, preferably in your Unit 3 folder, named 3.5 - Nested Loops.js.

Note - these will require prompt():

  • npm install prompt-sync
  • const prompt = require("prompt-sync")();

Emoji's might not work in your console. It seems to depend on the computer. If emoji's aren't working, you can use one of the characters from this website for the tasks below...

Task 1/2

  • print_line() - You will repeatedly ask the user how many emoji's (or interesting characters) to print on one line:

    1. Ask the user to enter a number.
    2. Print a line of emojis (of your choice) the length the user asked.
    3. Then ask them again.
    4. If they enter a negative number, the program quits.

    We will assume they are always entering a number.

    Emoji's can be found here
    Fun ASCII characters can be found here

    Just copy and paste into quotations "💩": console.log("☣")

    How many should I print? 5
    ☣☣☣☣☣
    How many should I print? 10
    ☣☣☣☣☣☣☣☣☣☣
    How many should I print? -3
    Bye!

💥 Want to take it to the next level? Give a menu so the user selects an emoji from a list and then ask them how many to print.


Task 2/2

  • print_square() - Very similar to #1 above, but this time you need to output a square of emoji's.
    For Example:
    How big is the square? 5
    ⚽⚽⚽⚽⚽
    ⚽⚽⚽⚽⚽
    ⚽⚽⚽⚽⚽
    ⚽⚽⚽⚽⚽
    ⚽⚽⚽⚽⚽
    How big is the square? 3
    ⚽⚽⚽
    ⚽⚽⚽
    ⚽⚽⚽
    How big is the square? -1
    Bye!

Challenges:

💥 Want to take it to the next next level? Print an outlined square with a different emoji in the middle print_outline():

⚽⚽⚽⚽⚽
⚽🏈🏈🏈⚽
⚽🏈🏈🏈⚽
⚽🏈🏈🏈⚽
⚽⚽⚽⚽⚽

💥 Okay, Smartypants... What about a diamond? print_diamond()

How wide is the diamond? 5
    ⚽
   ⚽⚽
  ⚽⚽⚽
 ⚽⚽⚽⚽
⚽⚽⚽⚽⚽
 ⚽⚽⚽⚽
  ⚽⚽⚽
   ⚽⚽
    ⚽


🐿️

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* The following examples might assist with your understanding
* of nested loops.
*
* It is also worth mentioning that you can nest ANY types of loops.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// Print weeks and days
function weekdays(num_of_weeks) {
for (let w = 1; w <= num_of_weeks; w++) {
// print the week heading
console.log("Week:", w);
for (let day = 1; day <= 7; day++) {
// print the day
console.log("\tDay:", day);
}
}
}
// Print the given word outter * inner times
function print_word(word, outter, inner) {
console.log("The word", word, "will print", outter*inner, "times:");
let counter = 1;
for (let i = 0; i < outter; i++) {
for (let j = 0; j < inner; j++) {
console.log(counter++, word);
/* It is possible to use math instead of
the 'counter' variable: i*inner + j */
}
}
}
// Print a multiplication table of size n x n
function multi_table(n) {
let output = "";
for (let outter = 1; outter <= n; outter++) {
for (let inner = 1; inner <= n; inner++) {
output += outter * inner;
output += "\t";
}
console.log(output);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment