1

I'm new to javascript and am having trouble with a counting program and I believe it could be trouble with variable scope.

var count = 0; {
    function gimmeRandom() {
        var rand = Math.floor(Math.random() * 10) + 1;
        count++;
    }

    function countToRandom() {
        for (count = 1; count <= rand; count++) {
            console.log(count);
        }
    }

    console.log("Counting to a random number");
    gimmeRandom();
    countToRandom();
    console.log("Counting to another random number");
    gimmeRandom();
    countToRandom();
    console.log("There has been " + count + " random numbers used");
}
2
  • 1
    There is an opening bracket at line 2, sure this is not a typo? Commented Mar 26, 2019 at 19:39
  • 1
    What goes wrong? What do you expect, and how do the results differ? edit and yes the code as posted is syntactically incorrect, so it's hard to answer the actual question. Commented Mar 26, 2019 at 19:39

3 Answers 3

1

You declare var rand inside gimmeRandom, you can't access it in countToRandom. You probably want a global variable, just as you did with count.

Sign up to request clarification or add additional context in comments.

Comments

0

Declaring var inside of a function scopes that variable to the function. You have two choices.

  1. Since you are calling gimmeRandom first, drop the var keyword and it will be automatically global.

var count = 0;     

{                                               
function gimmeRandom()                                              
{                                               
  rand = Math.floor(Math.random()*10)+1;                                                
  count++;                                              
}                                               

function countToRandom()                                                
{                                               
  for (count = 1; count <= rand; count++)                                               
  {                                             
     console.log(count);                                                
  }                                             
}                                               

console.log("Counting to a random number");                                             
gimmeRandom();                                              
countToRandom();                                                
console.log("Counting to another random number");                                               
gimmeRandom();                                              
countToRandom();                                                
console.log("There has been "+count+" random numbers used");                                                
}


  1. Define var rand at the top of the page to make it global.

var count = 0;     
var rand = 0;

{                                               
function gimmeRandom()                                              
{                                               
  rand = Math.floor(Math.random()*10)+1;                                                
  count++;                                              
}                                               

function countToRandom()                                                
{                                               
  for (count = 1; count <= rand; count++)                                               
  {                                             
     console.log(count);                                                
  }                                             
}                                               

console.log("Counting to a random number");                                             
gimmeRandom();                                              
countToRandom();                                                
console.log("Counting to another random number");                                               
gimmeRandom();                                              
countToRandom();                                                
console.log("There has been "+count+" random numbers used");                                                
}

3 Comments

This is basically what I needed but the the count variable isn't working properly. I think it's to do with the last line of code but I'm not too sure
@jonnyc132 what is count supposed to do? Right now it is doing exactly what you are telling it to do...
ideally I'd want it to show how many random numbers there have been, or how many times gimmeRandom has been called
0

You define rand to be bounded in this function:

    function gimmeRandom() {
         var rand = Math.floor(Math.random() * 10) + 1;
         count++;
    }

And then try to use it in another function:

    function countToRandom() {
        for (count = 1; count <= rand; count++) {
            console.log(count);
        }
    }

As seen in this question:

What is the scope of variables in JavaScript?

Your variable rand you define is given a local scope to the gimmeRandom() function and therefore can't be used outside that function.

To use it across functions you'd probably wan to make the variable have global scope

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.