Lottery numbers


#1

create a variable called customerNumbers and assign it a value
of 12.
Now, create an array called winningNumbers. When you have created it, use an
array object method to add the following six numbers:
12, 17, 24, 37, 38, 43

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.

can anyone help??? pleaseeeeeeeeeee :slight_smile:


#2

this is what i have so far

var customerNumbers = 12;

var winningNumbers = [12, 17, 24, 37, 38, 43];

alert("This Week's Winning Numbers are:\n" + winningNumbers + "\n" + "The Customer's number is:\n" + customerNumbers);

if (winningNumbers.indexOf(customerNumbers) != -1) {
    alert("This Week's Winning Numbers are:\n" + winningNumbers + "\n" + "The Customer's number is:\n" + customerNumbers + "\n" + "We have a match and winner!")
} else {
    alert("This Week's Winning Numbers are:\n" + winningNumbers + "\n" + "The Customer's number is:\n" + customerNumbers + "\n" + "Sorry you are not a winner this week")
}

#3

First of all we're not supposed to do your homework :wink:
Also what is your problem with it? Your code seems to work as expected, it's just that it completely ignores the task. Is this on purpose or do you have a problem to understand what the task asks you? We can help you with this, but doing your homework would be cheating, wouldn't it?


#4

I agree with @haxor789

It seems like you are missing a For loop.

Review your notes and add one

The basic syntax is this:

for (var i = start ; i < end; i++) {
//Do what you wanna do
}


#5

I am trying to do the for loop but I cant seem to get it working

var customerNumbers = 12;

var winningNumbers = [12,17,24,37,38,43];
var match = true;
for (var i = 0; i < winningNumbers.length; i++){
if (customerNumbers.indexOf(winningNumbers[i])=== -1){
	match = false;
	break;
}
}
if(!match){
	alert("This Week's Winning Numbers are:\n"+winningNumbers+"\n" +"The Customer's number is:\n"+customerNumbers+"\n" +"We have a match and winner!");
}
else {
	alert("This Week's Winning Numbers are:\n"+winningNumbers+"\n" +"The Customer's number is:\n"+customerNumbers+"\n" +"Sorry you are not a winner this week");
	}

#6

I am trying to do the for loop but I cant seem to get it working

var customerNumbers = 12;

var winningNumbers = [12,17,24,37,38,43];
var match = true;
for (var i = 0; i < winningNumbers.length; i++){
if (customerNumbers.indexOf(winningNumbers[i])=== -1){
match = false;
break;
}
}
if(!match){
alert("This Week's Winning Numbers are:\n"+winningNumbers+"\n" +"The Customer's number is:\n"+customerNumbers+"\n" +"We have a match and winner!");
}
else {
alert("This Week's Winning Numbers are:\n"+winningNumbers+"\n" +"The Customer's number is:\n"+customerNumbers+"\n" +"Sorry you are not a winner this week");
}

#7

Well first of all you still need to deal with this:

When you have created it, use an
array object method to add the following six numbers:
12, 17, 24, 37, 38, 43

And for the for loop: Well at first get rid of the indexOf, as said your first approach seem to work but the task seems to go a different way. Next you can get the value of an array at position x by using name[x-1] so for example the 3. value in the array would be winningNumbers[2] which is 24. Also you can directly compare them yourNumber === winningNumber[i].

And last but not least the break is nice but the task asks to use && (AND) in the condition of the for loop to deal with that case.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.