# Unable to get a compare function to work inside an object

212
January 28, 2018, at 10:35 AM

I have to make a lottery simulation. 6 numbers between 1 and 49 are to be drawn. A ticket with 6 chosen numbers from that ranged is to be compared with the lottery numbers. The data to be returned includes the drawn numbers, the numbers from the ticket and the count of matching numbers between the lottery draw and the ticket.

Note that I was instructed to create two classes, the first of which to generate random numbers. The second should have two methods. The first should draw the 6 numbers in the lottery. The second should take the 6 ticket numbers and compare the two pairs of numbers.

I have successfully compiled a function which conducts the draw. The numbers from the ticket are just presented in an array. I have a function which compares the two arrays, but it does not work in the code. I assume the reason is in the way the function is passed the numbers from the draw. If it is passed two random arrays, it works fine.

This is my attempt at the solution so far:

``````function Lottery() {
function Generator() {
var genNumber = Math.floor((Math.random() * 49) + 1);
return genNumber;
}
function Totalizer() {
this.drawNumbers = function DrawNumbers() {
var arr = []
while(arr.length < 6) {
var randomNum = Generator();
if (arr.indexOf(randomNum) > -1) {
continue;
}
arr[arr.length] = randomNum;
}
return arr.sort(function(a, b) {return a - b});
};
this.ticketNumbers = [16, 27, 7, 1, 17, 3].sort(function(a, b) {return a - b});
this.compNumbers = function CompNumbers(winNumbers, ticketNumbers) {
var winNumbers = this.drawNumbers();
var ticketNumbers = this.ticketNumbers;
var same = [];
for (var i = 0; i < winNumbers.length; i++) {
for (var j = 0; j < ticketNumbers.length; j++) {
if (winNumbers[i] === ticketNumbers[j]) {
same.push(winNumbers[i]);
}
}
}
return same.length;
}
}
var checkRes = new Totalizer();
console.log(checkRes.drawNumbers());
console.log(checkRes.ticketNumbers);
console.log(checkRes.compNumbers());
}
Lottery();
``````
POPULAR ONLINE

### JSZip files can't be unzipped

I want to download multiple files in a zip fileWhen the zip file downloads and I try opening it, I get the following error on macos 'unable to expand test into downloads (Error 1 - Operation not permitted)'

263

### Bind iron-page and and

I just began to learn the Polymer and at the moment to bind the paper-tabs and iron-pages together, so if I click the tab the content will be loaded dynamicallyAfter reading the documentation here is what I have at the moment

197

### Stop fetching chats in javascript chat tool works in chrome

I work on a chat tool which works on Javascript in chromeHow do I stop it fetching chats in chrome

274

### JavaScript for loop diffi

Here is the code I wrote:

246