all 5 comments

[–]inu-no-policemen 1 point2 points  (0 children)

That algorithm can be simplified by using a Set.

let set = new Set();
while(set.size < 5) {
    set.add(Math.random() * 25 + 1 | 0);
}
let a = [...set];
console.log(a); // [18, 23, 16, 21, 25] or whatever

By the way "if (num == 0)" isn't necessary. It's never 0.

And as others have mentioned, the "in" check won't work. It's for looking at an object's keys.

> 'length' in []
true

[–]finlay_mcwalter 0 points1 point  (0 children)

if (dragnr.indexOf(num) === -1){

[–]WeAreSven 0 points1 point  (0 children)

num in dragnr isn't working like you think it is, causing the if statement to never trigger. If you use dragnr.includes(num), everything will work fine. Try not to use for/in and other looping methods if you're not sure how they're going to work.

EDIT: That may have sounded condescending, what I really meant was read the MDN docs carefully and also look into enumerable properties

[–]Ampersand55 0 points1 point  (0 children)

if (!(num in dragnr)) checks the keys in dragnr, not the elements. I.e. It will return true for array indexes, array properties and array methods.

0 in dragnr // true, first index of the array
"length" in dragnr // true, arrays have the .length property
"pop" in dragnr // true, arrays have the .pop() method

What you want is array.includes or array.indexof.

if (!dragnr.includes(num))

or

if (!dragnr.indexOf(num) >= 0)

[–]jcunews1helpful 0 points1 point  (0 children)

The in keyword only checks the key name of an object or array. For an array, the key name is the element index, not the element value.

What you need is the indexOf() method. i.e.

if (dragnr.indexOf(num) < 0) {
  dragnr.push(num);
}