Homework Assignment: for Loop Challenge

Task: Create a program that does the following:

Initialize an array with the names of five of your favorite movies. Use a for loop to iterate through the array and print each movie name to the console. After printing all movie names, print a message indicating how many movies you listed.

Homework

Create a 3 by 3, 2D list that represents a tictactoe board. Use “X” for X, “O” for O, and None for empty tiles.

Ex. board = [[“X”,”None”,”O”],
       [“X”,”O”,”None”],
       [“O”,”None”,”X”]]

Iterate over the board and identify whether it is player X’s or player O’s turn.

Hint: count the number of moves(non-None). (X goes first)

Optional: use console.error() to report an error if the board is illegal (ex. 7 “X”s and 2 “O”s)

%%js

// 3x3 Tic-Tac-Toe board
let board = [
  ["X", "None", "O"],
  ["X", "O", "None"],
  ["O", "None", "X"]
];

// Function to check whose turn it is
function checkTurn(board) {
  let xCount = 0;
  let oCount = 0;

  // Iterate through the board and count X's and O's
  for (let row of board) {
    for (let cell of row) {
      if (cell === "X") {
        xCount++;
      } else if (cell === "O") {
        oCount++;
      }
    }
  }

  // Check for illegal boards (e.g., more X's than O's by more than one)
  if (xCount > oCount + 1 || oCount > xCount) {
    console.error("Illegal board: The number of X's and O's is incorrect.");
    return;
  }

  // Determine whose turn it is based on the counts
  if (xCount === oCount) {
    console.log("It's player X's turn.");
  } else if (xCount === oCount + 1) {
    console.log("It's player O's turn.");
  } else {
    console.error("Illegal board: The counts of X and O are invalid.");
  }
}

// Call the function to check the turn
checkTurn(board);

<IPython.core.display.Javascript object>

H.W application of While Loops.

Create the Outer Loop:

Use a while loop that runs while outerFactor is less than or equal to 10. Initialize the Inner Loop Variable:

Inside the outer loop, create another variable called innerFactor and set it to 1. Create the Inner Loop:

Inside the outer loop, use another while loop that runs while innerFactor is less than or equal to 10. Calculate the Product:

Inside the inner loop, calculate the product of outerFactor and innerFactor. Print the Product:

Print the product using console.log(), formatting the output neatly. Increment the Inner Loop Variable:

After printing the product, increment innerFactor by 1. Move to the Next Line:

After the inner loop finishes, print a new line to separate rows. Increment the Outer Loop Variable:

Increment outerFactor by 1 to move to the next row in the table.

%%js 

// Hw 2 (required)
// Initialize the outer loop variable
let outerFactor = 1;

// Outer loop: runs while outerFactor is less than or equal to 10
while (outerFactor <= 10) {
  
    // Initialize the inner loop variable
    let innerFactor = 1;

    // Inner loop: runs while innerFactor is less than or equal to 10
    while (innerFactor <= 10) {
      
        // Calculate the product
        let product = outerFactor * innerFactor;
        
        // Print the product, formatted neatly
        console.log(outerFactor + " x " + innerFactor + " = " + product);
        
        // Increment the inner loop variable
        innerFactor++;
    }
    
    // Print a new line after each row to separate rows
    console.log("\n");
    
    // Increment the outer loop variable
    outerFactor++;
}

<IPython.core.display.Javascript object>
%%js

// Hw 1 (Extra Credit)
// Disclaimer these are not my favourite movies, because I can barely think of one I like, so they're random.
// Initialize an array with favorite movies
const favoriteMovies = ["Inception", "The Matrix", "Interstellar", "The Dark Knight", "Parasite"];

// Use a for loop to iterate through the array and print each movie name
for (let i = 0; i < favoriteMovies.length; i++) {
    console.log(favoriteMovies[i]);
}

// Print a message indicating how many movies were listed
console.log("You listed " + favoriteMovies.length + " movies.");