1

I am wondering if this way is correct:

var userInput = confirm('roll die?');

var rollDie = function() {
    while(userInput) {
    var dieSide = Math.floor(Math.random() * 6);
        document.write('you rolled a ' + (dieSide + 1));


    userInput = false;
  }
}

rollDie(userInput);

Or do I need to write var rollDie = function(userInput) {

Colin Brock
  • 21,267
  • 9
  • 46
  • 61
user3152131
  • 543
  • 3
  • 7
  • 18

3 Answers3

2

Javascript works with scopes. You call the function 'rollDie' from a scope where 'userInput' is a variable. The function 'rollDie' has its own scope. In your example there's no variable 'userInput' in the scope of the function rollDie. There for javascript is looking for the variable in an outer scope and find the variable. So your program is working but the code is not good code.

because you call the function rollDie with the parameter 'userInput' you should add 'userInput' as param to the function rollDie. var rollDie = function(userInput) {} It is always better the give a function all the params the function needs to execute. This prefents problems with the 'this' scope in javascript when you call the function in an other context and make it easier to refactor your code.

twoStrars is quicker :)

Stefan van de Vooren
  • 2,524
  • 22
  • 19
2

You should understand the difference and then choose for yourself.

Basically, you have these two patterns:

x as global variable:

var x = 1;

var f = function() {
    console.log('x in f:', x);
    x = 2;
}

console.log('x before f:', x);
f();
console.log('x after f:', x);

and x as argument:

var x = 1;

var f = function(x) {
    console.log('x in f:', x);
    x = 2;
}

console.log('x before f:', x);
f(x);
console.log('x after f:', x);

There two main differences:

  1. if f uses a global variable, it is going to modify the global variable, whereas if it works with an argument, it does not affect any variables visible outside, i.e. the first code writes x after f: 2, whereas the second writes x after f: 1

  2. if f uses a global variable, then it becomes less convenient to pass it different values. With an argument, you don't even need a global variable, you can call f(1); f(2); f(3456);. With global vaiables, you would accomplish the same with var x=1; f(); x=2; f(); x=3456; f();.

Instead of going more into details, I'll give you a link: Why are global variables evil?

Anyway, there are cases when global variables are good! I would make a global variable for a value which is constant and used by multiple functions (var GRAVITY = 9.81; or var BASE_URL = "https://stackoverflow.com/";)

Community
  • 1
  • 1
zvone
  • 18,045
  • 3
  • 49
  • 77
1

This line:

rollDie(userInput);

…means you're trying to pass a value into your rollDie() method. This isn't strictly necessary because you have this variable declared globally:

var userInput = confirm('roll die?');

So, you could pass nothing in if you wanted, but if you want to write much cleaner code it's preferable to avoid having these global variables around as much as you can. The way you've written it – passing in a value to your function – is much nicer, so it's better to write var rollDie = function(userInput) {.

TwoStraws
  • 12,862
  • 3
  • 57
  • 71
  • thank you sir. i now realize this. what i have done is remove the confirm and put it into when calling the function only. – user3152131 Dec 19 '15 at 20:15
  • Great! The key is to keep your code split up neatly so it won't conflict with your other code too much. When you start small this isn't so much of a problem, but as your projects get big it gets much more important. Better to start off nice and neat so you have a firm grounding. – TwoStraws Dec 19 '15 at 20:16