# Random Number project

Random Number project
0

#1

Hey guys! Brand new coder here, been through a few courses and trying to work on a few things myself. I’m working on a code to generate 6 random numbers (all between 0 and 49). I can’t seem to get the randomNumber function to a) either return values to an empty winningNumbers array, or b) am lucky, its working, but can’t get it to print properly.

let lottoNumbers = {
_winningNumbers: [],
},

get winningNumbers() {
return this._winningNumbers;
}

randomNumber() {
for (number = 0; number < 6; number++) {
Math.floor(Math.random() * 49);
return this._winningNumbers;
}
}
};

const win = lottoNumbers.winningNumbers[];

console.log(win);

Any help is appreciated!

#2

Scratch the above, this is the code that’s getting the error. Its just printing the [ ] brackets.

``````let lottoNumbers = {
_winningNumbers: [],

get winningNumbers() {
return this._winningNumbers;
},

randomNumber() {
for (number = 0; number < 6; number++) {
Math.floor(Math.random() * 49);
return this._winningNumbers;
}
}
};

const win = lottoNumbers.winningNumbers;

console.log(win);
``````

#3

winningNumbers is a getter for a property. It returns the value of _winningNumbers which you initialized to a blank array with the following line:

``````  _winningNumbers: [ ],
``````

In the second code you posted, you are not calling lotoNumber’s randomNumber function. Even if you did not call it, it is still just going to return a blank array, because your for loop only iterates one time and the following line executes but is not assigned to a variable.

``````Math.floor(Math.random() * 49);
``````

FYI - Not sure if the 0 to 49 was supposed to include 0 and 49 or just 1,…, 48. The above evaluation will return numbers between 0 and 48 (both inclusive).

#4

You could modify your code as follows and use the same call.

``````let lottoNumbers = {
_winningNumbers: [],

get winningNumbers() {
this.randomNumber();
return this._winningNumbers;
},

randomNumber() {
for (number = 0; number < 6; number++)
this._winningNumbers.push(Math.floor(Math.random() * 49));
}
};

const win = lottoNumbers.winningNumbers;

console.log(win);  // [ 29, 48, 42, 13, 38, 25 ]
``````

#5

Thank you! This helps! I also got the “starting the random number at 1” problem sorted.

Thank you!