Store output of the function in a variable in javascript

I’m very new to javascript and this is my first individual project and I really exited about it,but some things don’t work properly yet. Problem I’m having is that I have this function that creates the random digit number :

//user_length() has a value of a favored length of number entered by user
//random_digit() returns digit randomly from 1-9



function random_number(){
      let sequence = [];
      for(i = 0; i < user_length() ; i++ ){
         sequence.push(random_digit());
      }

      return sequence.toString().replace(/,/g,"")
    }

but I have to call this function twice in different places:

 //to chack whether what user has written matches the displayed number or not
function check(){
    
    let inputVal = document.getElementById("user_input").value;
    
    for (i = 0 ; i < user_length() ; i++){
      return inputVal[i] ===random_number()[i] ? console.log(random_number()) : console.log(inputVal + " is wrong,right answer is " + random_number())
    }

   
  }

//to display number on screen for user to "remember"
function number_onScreen(){
    create_p();
    let random_num= document.getElementById("random_number");
    random_num.innerHTML = random_number();
    random_num.style.margin = "0px";
    random_num.style.padding = "0px";
    
  }

what happens is that number that is displayed on the screen is different from the number Check() function compares to the one user tried to remember. for example, the displayed number was: 23749

the user would also write 23749 if they remembered correctly, but the check() function has a different value from the displayed number and the user will never guess that. I understand that my problem is that I call the random_number() function two different times and that’s why they have different value, I tried to store the output in the variable but don’t really know how to so I always got "Undefined " as an output, could you help me?

each of the times the function is called it gives a different value

I think the value you are interested in keeping is this one, right?

instead of doing this, you need to save the output from random_number before this line, even outside of the function, as I don’t see check called here. What you can do is then to have that random number passed in as function parameter both to the function that display and the function that check

about your random_number function you can make it simpler if you already create a string instead of an array

let num = ""
// obtain random number as many times as you need 
num += random_digit

so you avoid all those converting array to string and stripping commas (join would also have worked there)

thanks for the reply,how do I save the output from the random_number?

you call it once, and store it in a variable, then use that variable instead of the function call

that’s the part I don’t get, I tried googling but wasn’t much of a use, should I assign whole function to variable or just the output, also could you show me the short example ?

var finalString = random_number();

is this what you mean ? how do I use variable later, do I call it just as finalString or finalString()

as you are storing the output of the function and not the function, itself it is finalString

function random_number() {
  let sequence = '';
  for (let i = 0; i < user_length(); i++) { // always declare all your variables, even those in loops
    sequence += random_digit();
  }
  return sequence;
}

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

1 Like