How do I make the variables global?


#1

Whenever I run the code below, I get NaNDraw, NaNAway, etc. I think it is because my variables such as myHome and others in that section are not global, how could I make them global.

var goalsHome = prompt ("Goals the Home Team has scored");
var missHome = prompt ("Goals the Home Team has conceded");
var goalsAway = prompt ("Goals the Away Team has scored");
var missAway = prompt ("Goals the Away Team has conceded");
var betHome = prompt ("The odds for the Home team");
var betAway = prompt ("The odds for the Away team");
var betDraw = prompt ("The odds for the teams to draw");

var myHome = function (number) { 
        var home = goalsHome/(goalsHome + missHome)*2*number;
};

var myDraw = function (number) { 
    var draw = 100-(goalsHome/(goalsHome + missHome))*2+goalsAway/(goalsAway + missAway)*2*number;
};
var myAway = function (number) {
    var away = goalsAway/(goalsAway + missAway)*2*number;
};

var totalHome = function (number) {
    var totalhome = betHome*myHome*number;
    console.log(totalhome+"Home");
};

var totalAway = function (number) {
    var totalaway = betAway*myAway*number;
    console.log (totalaway+"Away");
};

var totalDraw = function (number) {
    var totaldraw= betDraw*myDraw*number;
    console.log (totaldraw+"Draw");
};

myAway(1);
myHome(1);
myDraw(1);
totalAway(1);
totalHome(1);
totalDraw(1);

#2

No they should all be global from what I can see. What I think could be a problem is that prompt does always produce strings so a input of 42 would be turned into "42". Most of the times this doesn't matter as "42" has a numerical value of "42" but if for strings the + operator is "overloaded" with concatenation e.g. "5"+"5" = "55" and not 10. So be careful with prompt and maybe use parseInt("numerical string",10); or parseFloat("numerical string",10); to turn them into numbers.

Also for example here:

var totalhome = betHome*myHome*number;

myHome is the name of a function and [Function] times numbers is probably the reason why you get NaN. Probably this is the reason you ask for global variables, isn't it? You can here either create a variable home outside near to your betDraw variable or better use return here. Then you could use myHome(number) at this point. Is it clear what I mean?


#3

Sorry, I'm not quite following. I think I will just solve it by condensing the 6 functions into 3, for home, away and draw. Ill put the my's into the total, so the functions for the my's aren't their own function. That way I should not need to return myHome, etc. Thanks for the help.


#4

Sry I was debugging while already writing. There seem to be 2 issues with your code.

The first is just an advice, be careful when using prompted numbers. That's because prompt returns you always a string and for strings + means concatenation e.g. ""java"+"Script"-> "javaScript" which is fine for words but you don't want this for numbers as 5+5 should be 10 and not "55". For * and / it's not a problem as they aren't existing for strings so there is no ambiguity. The solution here is to use parseInt or parseFloat to turn them into numbers e.g. let's say betDraw is a numerical string "0.2" then

betDraw = parseFloat(betDraw,10);

would turn in from "0.2" to 0.2. For integer values such as "3" you'd use parseInt.

The second problem is that here:

var totalhome = betHome*myHome*number;

and a few other places you use a function name in a calculation. This means the value of myHome is [Function] which is pretty hard to calculate with so you end up at NaN. If you try to use home (the value computed in myHome) instead you'd run into the problem that this one is not global. So I suggested two solutions. The first one was to well make it global by e.g.

var home = 0;
var myHome = function (number) { 
        home = goalsHome/(goalsHome + missHome)*2*number;
};

now myHome would change the value of the global variable home.
And the second possibilty was to get rid of home and make use of return:

var myHome = function (number) { 
        return goalsHome/(goalsHome + missHome)*2*number;
};

now whenever you need the value computed in myHome you'd just ask for myHome(number) e.g.

var totalHome = function (number) {
    var totalhome = betHome*myHome(number)*number;
    console.log(totalhome+"Home");
};

better?


#5

Definitely, its clear now. Thanks for the help!