0

I have a simple function generating 4 random numbers from a getRandomNumber Function when called in a for loop. How can I prevent getting the same number twice I am adding elements to the DOm but it sometimes has 2 elements in the same div I want to prevent getting the same number twice. How can I do this? For example, getting for numbers so every time my code loads the numbers would be the same.

I was experimenting with if else statements and kind of backed myself into a corner. I know this may seem like a simple problem but I'm at my wits end on this one.

function getMeARandom(min, max) {
const randomNumber = Math.floor(Math.random() * (max - min + 1) + 
min);
return randomNumber;
}
function weaponsGen() {
for (let i = 0; i < 5; i++) {
    let weaponPosition = getMeARandom(0, 65);
}

}

  • 1
    So stop a second and consider what you are doing. You are generating a single number each loop iteration. And since the variable is a `let` inside the loop, it will be destroyed once the iteration is done. With that in mind, what do you need to do to make them persist so you can actually check that a duplicate is returned or not? – Taplar Jun 27 '19 at 11:37
  • Hi, @Taplar Thank you. I am fairly new to JS can you elaborate a bit more or give me an example if possible? – Vallis Weekes Jun 27 '19 at 12:34
  • Ref. [MDN let](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let) – Taplar Jun 27 '19 at 12:35

0 Answers0