skip to Main Content

I’m facing the issue when trying to return random number from the function.
Can anyone explain?

const MyButton = document.querySelector(".Flipper");

    MyButton.addEventListener("click", recordLog);

    function recordLog (){

        MyButton.style.backgroundColor = 'red';
            
        let RandomNumber = Math.floor(Math.random() * 256);

            
        return RandomNumber;

        console.log(RandomNumber);

    };

I suppose to get the random number and consol log it

2

Answers


  1. console.log won’t be printed after return statement. Change the function as follow

    function recordLog (){
    
            MyButton.style.backgroundColor = 'red';
                
            let RandomNumber = Math.floor(Math.random() * 256);
    
            console.log(RandomNumber);
    
            return RandomNumber;
    
    
    
        };
    
    Login or Signup to reply.
  2. Any statement after return in a function does not get executed because it marks the function as end, you would have to put all the things before return in order to some them, generally return is at the last of the function

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search