JavaScript, Lesson 5 - Homework QuestionsQ1Write a program that creates an array with four rows and four columns, and then asks the user to enter values for all the elements. The program should then "rotate" the array clockwise, as shown in this example:
Q2Twelve children (Jim, Sandra, Freddie, Richard, Diane, Mary, Natalie, Jeremy, Henry, William, Sarah and Charles) are to sit at desks in a rectangular arrangement (3 rows of 4 columns of desks). However, William and Jim must sit in the front row (nearest the teacher) because they tend to mess around in class. Sarah must not sit next to Diane as they tend to copy each others' work. Henry must not sit adjacent to (within one space of in any direction) Freddie or Natalie and Sandra and Jim must not be in the same row. Write a program which asks for a seating plan from the user and checks to make sure it meets those restrictions. The program should then display the seating plan. Q3Write a computer program that allows two human players to play noughts-and-crosses (tic-tac-toe) against each other. The program should detect when either player has won, or when the game is a draw. The board position can be displayed in terms of words (as at the moment you probably don't know how to draw the board in terms of pictures yet). Q4Create a three-dimensional array which represents a section of the galaxy. Your array should have 10 rows, 10 columns and 10 layers. The star ship Enterprise is at position (1, 3, 4), a Klingon battleship at (7, 9, 7) and stars at (1, 3, 9), (4, 2, 7), (2, 7, 3), (8, 5, 6) and (7, 2, 5).
Q5A magic square is a square grid full of numbers where each row, each column and each of the two diagonals adds to the same number. For instance, a 3-by-3 magic square containing the numbers 1, 2, 3 etc. up to 9 will have rows, columns and diagonals where the numbers add to give 15. Write a program which allows the user to enter the numbers, and then reports whether the array forms a magic square. More importantly, the user should be able to alter the numbers in the array, with the program reporting the sums of the rows, columns and diagonals, and then print a congratulatory message when the array finally is a magic square. Q6A prime number is one which can only be divided by two whole numbers - 1 and itself. Hence 7 is a prime number because the only whole numbers that divide into it are 1 and 7. On the other hand 8 is not a prime number because it also divides by 2 and 4. The first prime number is 2, not 1!
The ancient Greek mathematician Eratosthenes developed this method for finding all the prime numbers up to a certain limit (say 100):
Write a program that allows the user to enter an upper limit (it needn't be 100) and then displays all the prime numbers up to that limit using this "Sieve of Eratosthenes". |