Having trouble functions sharing variables. (Math.random variable)

I was not sure what to put in topic i hope this is good enough.

I’m currently having a bit of a problem. When i press a button on my website a function is called, more specificly this function :

function addWord(){

    var ri = Math.floor(Math.random() * glosorEng.length);
    document.getElementById("ID").innerHTML = myArray[ri];


And then after the user has clicked this button, they will type something in an input element and trigger another function that looks like this :

function Check_if_true(){

    var userEnter = document.getElementById("userAnswer").value;


     if(userEnter == myArray2[ri]){
         alert("Correct Answer!!!");
    else {
        alert("Wrong Answer, try again!");


The code works so far, it does everything it should, however if the user presses the button that triggers the first function again and again… the second function still only return true if the user has the correct answer of the first thing that popped up when activating the first function.

I think i can solve this if i can somehow make the ri variable global so that both functions always have the same valuable. The problem of just creating this variable outside the function inorder to make it global that way is that i want it to be generated when the user clicks.

Appreciate any guidance i can get, i understand if you need to ask further questions because i feel i made this a bit messy sadly. Anyhow, cheers.

Just a stylistic note here, make sure to wrap your entire function, including the name and ending bracket in the triple back-tics to make it a little more readable. Also, based on your brackets, it seems like addWord wraps the for loop, so the ri should be accessible within it.

I think your mistake is iterating through myArray using the variable i, but each time you’re checking some second myArray2 with the unchange variable ri. Try:

if(userEnter == myArray[i]){
    alert("Correct Answer!!!");
} else {
    alert("Wrong Answer, try again!");

It might help if you added more context as well, for variables like glosorEng and myArray. I think you want to use ri to access glosorEng not myArray.

Hey! Thanks for you reply. No the addWord function does not wrap around the loop, i made a misstake when i wrote the thread late last night. The second function i am talking about is only wrapping the for loop.

Everything works well with what i got in my JS file, except that the second function does not understand that when the first function runs again the correct answer also changes, it will still only accept the number that was generated the first time in ran until i update the website with lets say F5.

Cheers! :slight_smile:

I think my original comment had a mistake in the code as well. Please review, and see if that helps.

Beyond that, I think you need to post your entire code file here. It’s difficult to debug without the definitions for myArray, myArray2, and glosorEng.

A global will work fine, but you can encapsulate the logic. Example using a class (the instance of RandGenerator would be global):

class RandGenerator {
  constructor(glosor) {
    this.ri = undefined;
    this.glosor = glosor;

  generate() {
    this.ri = Math.floor(Math.random() * this.glosor.length);
    return this.ri;

Then use like

const myRandGen = new RandGenerator(glosorEng);

// Generate a new randomish value
// Access it
// You can keep using it until you need a new one,
// When you just run generate() again