0

I am trying to build a table with 15 random numbers as part of a project to make the 15 puzzle game in html/javascript. I am going to insert random non-repeating numbers from an array in the first 15 spots of a table. I can't seem to get the alert with the array to work. Any help would be appreciated. Thank You!

EDIT: now i am still getting some repeating numbers. i don't understand why since im pretty sure the array is being checked for duplicates.

<script>

    var arr = [];
    function roll() {

        for (i = 0; i < 15; i++) {
  var randomNumber = Math.floor(Math.random() * 15) + 1;
            if (arr.indexOf(i) != randomNumber) {
                arr.push(randomNumber);

            }
        } alert("This Works" + arr);
    }

</script>
Sollus
  • 11
  • 1
  • 6

8 Answers8

0

It is because of it went never end loop. You can see the error in your console like Out of stack space. You have called the roll function on your if condition. That if condition will never fails as per your calculation using Math. Remove method call and try to run, you will get the alert.

var arr = [];
function roll() {
    var randomNumber = Math.floor(Math.random() * 15) + 1;
    for (i = 0; i < 15; i++) {
        if (arr[i] != randomNumber) {
            arr[arr.length] = randomNumber;               
        }
    } alert("This Works" + arr);
} 

DEMO

Suresh Ponnukalai
  • 13,820
  • 5
  • 34
  • 54
0

Use arr.indexOf to check the element is present in array. And don't use recursive function and loop together for a single dimensional array manipulation. Always use arr.push() method to insert into an array which is best practice

var arr = [];

function roll() {
  for (i = 0; i < 15; i++) {
    var randomNumber = Math.floor(Math.random() * 15) + 1;
    if (arr.indexOf(randomNumber) < 0) {
      arr.push(randomNumber);
    } else i--;
  }
  alert("This Works " + arr);
}
roll();
jafarbtech
  • 6,842
  • 1
  • 36
  • 55
0

You cant see alert to work because you are not calling your roll() function anywhere . Please see below the code. You have many error in your code as well like the function is Math.floorand i is undefined in your code , also remove the function call roll inside roll function as its a never ending loop or you have to put the function call in a condition to make it work.

Please see below the corrected code.

<script>

var arr = [];
function roll() {
    var randomNumber = Math.floor(Math.random() * 15) + 1;
    for (var i = 0; i < 15; i++) {
        if (arr[i] != randomNumber) {
            arr[arr.length] = randomNumber;
        }
    } alert("This Works" + arr);
}
roll();

vertika
  • 1,284
  • 1
  • 8
  • 22
0

You have a typo. It should be Math.floor instead of Math.Floor.

You're not breaking the recursive call anywhere. So it will make a max call stack size exceeded error.

I used Array#find instead of traditional for loop in the below example.

var arr = [];

function roll() {
  if(arr.length>=15){
    console.log(arr);
    return;
  }
  var randomNumber = Math.floor(Math.random() * 15) + 1;
  if(arr.find(elm => elm == randomNumber)==undefined){
    arr.push(randomNumber);
   }
   roll();
}
<button onclick="roll();">Click</button>
Sagar V
  • 12,158
  • 7
  • 41
  • 68
0

//here is the code that may help you to solve the issue

    var arr = [];
    function roll() {

        for (; ; ) {
            var randomNumber = Math.floor(Math.random() * 15) + 1;
            if(arr.indexOf(randomNumber) == -1){
                 arr[arr.length] = randomNumber;
                 if(arr.length == 15)
                    break;
            }
        } 
        alert(arr)
    }
    roll(); 
Tushar Ghosh
  • 942
  • 1
  • 12
  • 18
0

You may try this

var arr = [];

function roll() {
  for (i = 0; i < 15; i++) {
    var randomNumber = Math.floor(Math.random() * 15) + 1;
    if (arr.indexOf(randomNumber) < 0) {
      arr[arr.length] = randomNumber;
    }
  }
  alert("This Works " + arr);
}
roll();
Vignesh
  • 1,140
  • 1
  • 9
  • 17
0

You're getting duplicates because you are not checking new numbers agains all existing numbers. Hope this helps:

var arr = [];
function roll() {
  var randomNumber;
  do {
    randomNumber = Math.floor(Math.random() * 15) + 1;
    if (!arr.includes(randomNumber)) {
      arr.push(randomNumber);
    }
  } while (arr.length < 15)
  alert("This Works: " + arr);
}
roll();
EJAg
  • 3,210
  • 2
  • 14
  • 22
  • omg thank you, i swapped out the if statement and put the i++ after the push it is working perfectly. thank you – Sollus Oct 11 '17 at 05:26
0

Using tail recursion:

function roll(size, arr) {
    arr = arr || [];
    if(size <= 0) return arr;
    return roll(size-1, arr.concat([Math.ceil(Math.random() * 15)]));
}

console.log("This Works: " + roll(15));
console.log("This Works: " + roll(10, []));

Math.floor(...) + 1 has become Math.ceil(...) as both are the same.

Sidtharthan
  • 197
  • 9