Somewhere in your code you invoked the function gerarDezenas()
? For the behavior of a function to be executed, it is necessary that you invoke it. For this we write the function name and a pair of parentheses at the end.
// Invocando função
gerarDezenas();
Another tip is about indentation. Whenever you open keys, remember to indent the code that is inside the keys in another level. That is, when you declare a function, a conditional, a bond... as you open new structures you enter further, improving understanding. It’s easier to see that that block belongs to that structure. Your code with the correct indentation would look like this:
function gerarDezenas() {
var dezenas = ["d1","d2","d3","d4","d5","d6"];
for (var i = 0; i < dezenas.length; i++) {
dezenas[i] = Math.round(Math.random() * 60 +1); // isso é correto?
console.log(dezenas);
}
}
Now about the code itself. The function Math.random()
generates a number between 0 and 1, exclusive to 1, that is, up to 0.9999... (infinity nines). So if you take this and multiply it by 60, you will get a number between 0 and 59,9999... If you use only the Math.round()
on top of that, without that +1, it would generate a number between 0 and 60. As you added 1, it would result between 1 and 61.
Also note that you are doing
console.log(dezenas)
This will print your entire dozens list, will have the following output:
['d1', 'd2', ..., 'd6' ]
I believe what you wanted to do was to print the ith element of the list of dozens, that is,
console.log(dezenas[i])
That will print a different string with each loop iteration.
Finally, whether a function returns or not (void function) depends on the type of behavior you expect from it. If you want the function to produce some result, surely it will have return. If you just want her to perform an action, or cause a side effect, it will be void. Its function, for example, could be done in both ways:
- Returning a list of random dozens.
- Printing a list of random dozens.
This answers your question? Generate multiple random numbers without repetition
– hkotsubo