External Exam Download Resources Web Applications Games Recycle Bin

Loops

forLoop.js

for (counter = 1; counter < 5; counter++){
    alert("Counter value is: " + counter);
}
alert("For-loop is now finished.");

doLoop.js

passengers = 7;

do {
    alert("Passengers remaining: " + passengers);
    passengers = passengers - 1;
}
while (passengers > 0);
    
alert("No passengers remaining.");

mathsGame.js

//random number between 1 and 9:
randomNumber = Math.floor((Math.random() * 9) + 1);

userGuess = prompt("Can you guess my number? Have a go: ");

/*
By now you should be sick of using alerts and prompts.
The next step is to learn how the DOM works.
*/
    
while (userGuess != randomNumber){
    if (userGuess < randomNumber){
        alert("My number is higher than your last guess.")
    }
    else{
        alert("My number is lower than your last guess.")
    }
    userGuess = prompt("Try another guess: ");
}
    
alert("Congratulations, you guessed my number!");

Try questions 1, 2 and 3 with a for loop, a while loop, and a do while loop:

  1. Write a program that uses a loop to count down from 10 to 1. After the number 1, the program should say "Blastoff!"

  2. Write a program that uses a loop to alert all the even numbers between 2 and 30.

  3. Write a program that uses a loop to tally up (i.e. sum) all the odd numbers between 70 and 60.

  4. Add loops and scoring to previous Paper Rock Scissors game.

  5. List all the prime numbers between 1 and 21.


//random number between 1 and 3:
computerGuess = Math.floor((Math.random() * 3) + 1);
computerHand = "";
switch(computerGuess){
    case 1:
        computerHand = "PAPER";
        break;
    case 2:
        computerHand = "ROCK";
        break;
    case 3:
        computerHand = "SCISSORS";
        break;
}
alert("Computer chose: " + computerHand);

winner = "";    

//random number between 1 and 3:
userGuess = Math.floor((Math.random() * 3) + 1);
userHand = "";
switch(userGuess){
    case 1:
        userHand = "PAPER";
        switch(computerHand){
            case "PAPER": winner = "Draw"; break;
            case "ROCK": winner = "User"; break;
            case "SCISSORS": winner = "Computer"; break;
        }
        break;
    case 2:
        userHand = "ROCK";
        switch(computerHand){
            case "PAPER": winner = "Computer"; break;
            case "ROCK": winner = "Draw"; break;
            case "SCISSORS": winner = "User"; break;
        }
        break;
    case 3:
        userHand = "SCISSORS";
        switch(computerHand){
            case "PAPER": winner = "User"; break;
            case "ROCK": winner = "Computer"; break;
            case "SCISSORS": winner = "Draw"; break;
        }
        break;
}
alert("User chose: " + userHand);
alert("Winner: " + winner);


numberOfDivisors = 1;
for(x=1;x<=21;x++){
    for(y=x; y>1; y--){
        if(x % y == 0){
            numberOfDivisors++;
        }
    }
    if(numberOfDivisors < 2){
        alert(x + " is prime");
    }
    numberOfDivisors = 0;
}