Homework Assignment: For Loop Challenge

Task: Create a program that does the following:

  • Fix and improve a loop example shown in class
  • Include at least two different loop conditions
  • Code has to run without any error
  • Must be submitted before next class using .ipynb format

Example from class:

let fruits = ["Heart Shaped Herb", "Yami Yami no Mi", "Gomu Gomu no Mi"];

for (let i = 0; i < fruits.length; i++) {
    console.log(fruits[i]);
}

Solution

I improved the example by using different fruits, adding numbering in the first loop, and using uppercase in the second loop. Included a for loop and a for-of loop.

// CODE_RUNNER: Iterations Homework


// Fixed and improved loop example
let fruits = ["Apple", "Banana", "Orange"];

// First loop: for loop with numbering
console.log("Fruits using for loop:");
for (let i = 0; i < fruits.length; i++) {
    console.log(`${i + 1}. ${fruits[i]}`);
}

// Second loop: for-of loop with uppercase
console.log("Fruits using for-of loop:");
for (let fruit of fruits) {
    console.log(fruit.toUpperCase());
}