0

I have this code, where the point is to read lottery numbers, and the numbers can not be smaller than 0, nor bigger than 49, nor can they repeat themselves. I don't understand how to put that in the while loop. How can I compare each number is inserted with the numbers previously inserted?

var totoloto=new Array(1);
for(var i=0; i<1; i++) {
    totoloto[i]=new Array(5);
    for(var j=0; j<5; j++) {
        do {
            totoloto[i][j] = parseInt(readLine("totoloto="));
        } while (totoloto[i][j] < 1 || totoloto[i][j] > 49);
    }
    print(totoloto[i].toString().replaceAll(",", " "));
}
RandomName
  • 23
  • 4
  • Use "includes" like answered in https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript – Abdurrahim Nov 29 '18 at 20:14

1 Answers1

0

You can use Set instead of Array and just add values into set. If you don't know, Set contains only unique values. Another way to do this is just using object:

let obj={}
obj.value1 = true // true is just for example. It doesn't make any sense
obj.value2 = true
// After getting all the keys you can
Object.keys(obj) // it returns all the keys in objecti i.e. your values

So, adding values with the same keys has no effect, because object can have only unique keys.