samedi 13 janvier 2018

Add a compound AND condition that allows the loop to continue to iterate only if

I am doing an exercise, these are the requirements:

Use a For loop to step through each position in the winning numbers array and to compare the customer number to each number the array contains.

To complete this, you will need to set up the following.

  1. A counter variable (e.g. i) for the loop.
  2. A boolean variable (e.g. match) to flag if a match has been found or not.
  3. A compound AND condition that allows the loop to continue to iterate only if a match is not found, and, the end of the array has not been reached.
  4. An if statement nested inside the For loop which checks the customer number against each winning number in the array, each time the loop iterates, and sets the boolean, match, to true if a match is found.

What I have so far works but I do not understand where requirement 3 would go or the need for it, could someone please explain?

What I have so far:

var customerNumbers = 12;
var winningNumbers = [];
var match = false;

// Adds the winning numbers to winningNumbers
winningNumbers.push(12, 17, 24, 37, 38, 43);

// Messages that will be shown
var winningMessage = "This Week's Winning Numbers are:\n\n" + winningNumbers + "\n\n";
var customerMessage = "The Customer's Number is:\n\n" + customerNumbers + "\n\n";
var resultMessage = "Sorry, you are not a winner this week.";

// Searches the array to check if the customer number is a winner
for (var i = 0; i < winningNumbers.length; i++) {
        if (customerNumbers == winningNumbers[i]) {
                resultMessage = "We have a match and a winner!"
                match = true;
        }
}

// Result
alert(winningMessage + customerMessage + resultMessage);        

Aucun commentaire:

Enregistrer un commentaire