Skip to main content
tomc69945220
Participant
July 25, 2017
Question

push to array help needed

  • July 25, 2017
  • 1 reply
  • 82 views

Hi, I'm hoping someone can help.

I'm trying to build a custom interaction with JS but having a few issues, and bare in mind JS is totally new to me so I'm flying blind at the moment.

Basically, what I want is a slide button that will take a user to a random slide from a choice of 5, the result of the random output to be added to an array. User then returns to original slide and proceeds onto another random slide, checking the array to make sure of no duplicates. After the user has visited 3 random unique slides the function will continue onto other actions which I haven't written yet.

Directing users to the random slides is fine, the issue I'm having is that it's not pushing new values to the array, just overwriting them instead. I've not continued with the code yet as I don't see the point until I can get this first bit working properly, but I'll add what I've done below.

Hopefully this made sense, but if not let me know and I'll try to clarify.

var ls_alreadyDone = [];

var num = Math.random();

var game = function() {

  if (num <= 0.2) {

  rdcmndGotoSlide = 1;

  ls_alreadyDone.push(1);

  } else if (num > 0.2 && num <= 0.4) {

  rdcmndGotoSlide = 2;

  ls_alreadyDone.push(2);

  } else if (num > 0.4 && num <= 0.6) {

  rdcmndGotoSlide = 3;

  ls_alreadyDone.push(3);

  } else if (num > 0.6 && num <= 0.8) {

  rdcmndGotoSlide = 4;

  ls_alreadyDone.push(4);

  } else if (num > 0.8) {

  rdcmndGotoSlide = 5;

  ls_alreadyDone.push(5);

  }

};

game();

console.log(num);

con

so

(ls_alreadyDone);

    This topic has been closed for replies.

    1 reply

    tomc69945220
    Participant
    July 25, 2017

    N.B. the last line messed up on here for some reason, in my code it's fine though, just console.log(ls_alreadyDone); that I used for testing.