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");
}
Pointy
  • 405,095
  • 59
  • 585
  • 614
jonnyc132
  • 7
  • 1
  • 1
    There is an opening bracket at line 2, sure this is not a typo? – Charis Moutafidis Mar 26 '19 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. – Pointy Mar 26 '19 at 19:39

3 Answers3

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.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
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");                                                
}
basic
  • 3,348
  • 3
  • 21
  • 36
  • 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 Mar 26 '19 at 19:50
  • @jonnyc132 what is count supposed to do? Right now it is doing exactly what you are telling it to do... – basic Mar 26 '19 at 19:56
  • ideally I'd want it to show how many random numbers there have been, or how many times gimmeRandom has been called – jonnyc132 Mar 26 '19 at 20:43
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

chevybow
  • 9,959
  • 6
  • 24
  • 39