user5049054
user5049054

Reputation:

JS array splice don't remove the index for some reason

This is the code that I'm sending :

for (var com = 0; com < 10; com++) {
        var op = [0, 1, 2, 3];
        combo = op[Math.floor(Math.random() * op.length)];
        door1[com] = combo;
        op.splice(combo, 1);
        combo = op[Math.floor(Math.random() * op.length)];
        door2[com] = combo;
        op.splice(combo, 1);
        combo = op[Math.floor(Math.random() * op.length)];
        door3[com] = combo;
        op.splice(combo, 1);
        combo = op[Math.floor(Math.random() * op.length)];
        door4[com] = combo;
        eim.print(op.toString());
}

Now when I print the op array. I should get only 1 value because I've deleted already the other indexes. with the splice. but when I print the result 10 times. I find that some of the indexes didn't removed. I'm trying to create 4 different combos. Each combo is different from the other one.

Upvotes: 0

Views: 80

Answers (3)

Nina Scholz
Nina Scholz

Reputation: 386654

Just take the spliced element for the door arrays. Array.prototype.splice returns an array, so for just one, take the first element, with index [0].

The last assignment for door4 does not need a calculation of random or so, just take the single element of the array op.

var combo, com, door1 = [], door2 = [], door3 = [], door4 = [], op;
for (com = 0; com < 10; com++) {
    op = [0, 1, 2, 3];
    door1[com] = op.splice(Math.random() * op.length | 0, 1)[0];
    door2[com] = op.splice(Math.random() * op.length | 0, 1)[0];
    door3[com] = op.splice(Math.random() * op.length | 0, 1)[0];
    door4[com] = op[0];
}
document.write('door1: ' + door1 + '<br>');
document.write('door2: ' + door2 + '<br>');
document.write('door3: ' + door3 + '<br>');
document.write('door4: ' + door4 + '<br>');

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

You need to use the index of the item to remove it from the array, not the value

var door1 = {},
  door2 = {},
  door3 = {},
  door4 = {};
for (var com = 0; com < 10; com++) {
  var op = [0, 1, 2, 3];
  door1[com] = getValue(op);
  door2[com] = getValue(op);
  door3[com] = getValue(op);
  door4[com] = getValue(op);
  snippet.log(JSON.stringify(op));
  console.log(op)
}

function getValue(array) {
  var idx = Math.floor(Math.random() * array.length),
    val = array[idx];
  array.splice(idx, 1);
  return val;
}
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Upvotes: 0

Naeem Shaikh
Naeem Shaikh

Reputation: 15715

what you are doing is ,

Array.splice(combo,1);

now what it is trying to do is to splice an element from array whose index is combo, now combo can be any value, and it is not index.

you can try it by index, FIDDLE

code:

door1={}
door2={}
door3={}
door4={}
var index=null;
for (var com = 0; com < 10; com++) {
        var op = [0, 1, 2, 3];
        index=Math.floor(Math.random() * op.length)
        combo = op[index];
        door1[com] = combo;
        op.splice(index, 1);
        index=Math.floor(Math.random() * op.length)
        combo = op[index];
        door2[com] = combo;
        op.splice(index, 1);
        index=Math.floor(Math.random() * op.length)
        combo = op[index];
        door3[com] = combo;
        op.splice(index, 1);
        index=Math.floor(Math.random() * op.length)
        combo = op[index];
        door4[com] = combo;
        document.write(op.toString()+'<br>');
}

Upvotes: 2

Related Questions