0

I've been trying to write a go back function. What this function will do is it'll store last two ID number that has been generated and append when there is a new one, delete the first one.

I have this function which creates the IDs and plays the video with that ID.

function newVideo(){
  let rando = Math.floor((Math.random() * 3970) + 1);
  document.getElementById("vsrc").src = "https://raw.githubusercontent.com/ondersumer07/vinematik-videos/master/vid/" + rando +  ".mp4";
  document.getElementById("videoEl").load();
  return rando;
}

I am returning the rando to use it outside this function, and I can access it outside the function, the problem is, the variable outside the function is not updating everytime newVideo() run.

The code for that goes like this:

let rando = newVideo();
let vidids = [];

if (vidids.length < 2) {
  vidids.push(rando)
  console.log("added")
} else {
  vidids.shift()
  console.log("deleted")
};

Basically what this does is to get the returned rando value and push it to the vidids array, delete the first one if there is more than two but it won't, it does not update the let vidids = []; array for some reason. I need it to update everytime the newVideo() function ran.

Also, I want the if to add if there is less then two items in that array and delete from the start of the array if there is more than two items in it. Not really sure if that'll work too.

I can't seem to figure out how to do this, am I doing this whole thing wrong or is there still hope for this function? How am I supposed to write that function?

Edit: I've changed the vidids.length part yet the problem still occur because let rando = newVideo(); doesn't update.

Önder Sümer
  • 141
  • 1
  • 9
  • You have typo. It should be `if (vidids.length < 2)` as you are checking for array length – palaѕн Mar 19 '20 at 10:40
  • Oh, thank you. I've missed that one but still that does not solve the problem of array not updating. My if fucntion still adding one to the array as it is not updatig the the vidids array. Thank you tho, this would still be problem. – Önder Sümer Mar 19 '20 at 10:43

2 Answers2

0

In order to keep the last two Items of an array, you can use something like this.

let vidids = [];

function addToArray(item, array){

    array.unshift(item);
    return array.slice(0,1);

}



//test
    vidids = addToArray(newVideo(), vidids);
    vidids =addToArray(newVideo(), vidids);
    vidids =addToArray(newVideo(), vidids);
    vidids =addToArray(newVideo(), vidids);
    vidids =addToArray(newVideo(), vidids);
    console.log('Done');

You've made a small mistake that caused your code to not work properly. The IF condition is intended to check the length of the 'vidids' array, What actually happens is that it compares the array referance instead of it's length

To fix the issue, add .length after 'vidids' inside of the IF condition.

....
if (vidids.length < 2) {
.....
Adi Darachi
  • 2,137
  • 1
  • 16
  • 29
  • Yep, thank you. I've actually changed the code but this does not seem to solve the main problem as written [Here](https://stackoverflow.com/questions/60755376/how-to-update-a-variable-that-is-returned-from-a-function?noredirect=1#comment107492617_60755376) – Önder Sümer Mar 19 '20 at 10:47
  • I've updated the answer, is that you wanted to achive? – Adi Darachi Mar 19 '20 at 11:02
  • Thank you for your update, I was trying the code. Your update still does what it needs to do yet does not solve my main issue. It only keeps the first ID that has been returned and doesn't keep the other ones after that. The array is not being updated when the newVideo() function run. – Önder Sümer Mar 19 '20 at 11:04
0

I figured it out. Basically what I did is storing every number the algorithm creates and then taking one before the last.

Which goes like this:

The algorithm for creating random numbers:

  function randomNum() {
  let rando = Math.floor((Math.random() * 3970) + 1);
  return rando;
};

Then I put this function in a variable:

let videoid = randomNum()

I had another variable called videoids which is above and outside of the randomNum function and is an array:

let videoids = []

After that I stored every number I created with videoid inside videoids by pushing it(This push needs to be inside your function):

videoids.push(videoid);

Okay so I stored all the numbers this way. Now I should take one before the last so I can go to previous video. So I needed to create a function, I used this function which was created by Tadeck, in this thread.

if (!Array.prototype.last) {
  Array.prototype.last = function() {
    return this[this.length - 2];
  };
};

Now I can put that inside of my prevVideo function which looks like this:

function prevVideo() {
  document.getElementById("vsrc").src = srcRaw + videoids.last() + ".mp4";
  document.getElementById("videoEl").load();
  videoids.push(videoids.last());
}

Note: Don't forget to push videoids.last inside your videoids otherwise you can only go to your previous number for once.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Önder Sümer
  • 141
  • 1
  • 9