0

I haven't done Javascript in years and I'm trying to get back into it. I just wanted to create a couple random numbers and add them together. I also wanted to practice using "getVariable()" to protect manipulation of the original variable while making them accessible to the entire script. I must be remembering this wrong or I'm doing something really stupid. I keeping getting a message that getScore1 is undefined. I tried writing it as function getScore1() and this.getScore1 = function(){}. Can someone please point me in the correct direction?

function twoRandomScores(){
    var score1 = Math.random(1, 10);
    var score2 = Math.random(1, 10);
    return score1 + score2;
    this.getScore1 = function(){
            return this.score1;
        }
    function getScore2(){
            return this.score2;
        }
}

document.write(getScore1() + '+' + getScore2() + '=' + twoRandomScores());

3 Answers3

2

The getScore functions are defined inside your twoRandomScores() function so they won't simply be accessible from outside of it. The code as you've written it now doesn't really make sense either because the getScore() functions would only have any meaning after twoRandomScores() was called (and for one particular call of it). Here's one way you could approach this problem:

function twoRandomScores(){
    var score1 = Math.random(1, 10);
    var score2 = Math.random(1, 10);
    return {
        score: score1 + score2,
        getScore1: function(){
            return score1;
        },
        getScore2: function(){
            return score2;
        }
    };
}

var scores = twoRandomScores();
console.log(scores.getScore1() + '+' + 
            scores.getScore2() + '=' + 
            scores.score);

Then again, having two functions for getScore1, getScore2 doesn't really accomplish anything, so you could just do:

function twoRandomScores(){
    var score1 = Math.random(1, 10);
    var score2 = Math.random(1, 10);
    return {
        score: score1 + score2,
        score1: score1,
        score2: score2
    };
}

var scores = twoRandomScores();
console.log(scores.score1 + '+' + scores.score2 + '=' + scores.score);
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • Thank you! This is the explanation I was looking for. The point of getScore function within a function is to access a private variable without subjecting it to changes. I remember the point I just forget the code exactly. I'll have to look that part up somewhere. Thank you for clarifying the rest. – user2852792 Jan 24 '15 at 21:01
1

Sure that your code is correct? Before assigning getScore1, you are returning out of the function, so the assignment to getScore1 and getScore2 never occurs. Hence the undefined error...

  • `getScore2` is hoisted but `getScore1` isn't. The main issue is about scope I'd say, but that's a good point. – elclanrs Jan 24 '15 at 20:50
  • Thanks - Good point. I tried moving it, but I still get "Uncaught ReferenceError: getScore1 is not defined" in the console. – user2852792 Jan 24 '15 at 20:51
  • You'd probably need to move the this.getScore1 = function ()... etc parts outside of the twoRandomScores() function, but then that leaves a need to define 'var score1' outside of the twoRandomScores() function as well... Or follow the other provided answers :p – zaSmilingIdiot Jan 24 '15 at 20:59
1

You're mixing up a regular function with the type of function used to create an object. This is how it might work if you wanted to make an object (JSFiddle)

function RandomScore() {
   var score1 = Math.random() * 10,
       score2 = Math.random() * 10;

   // make functions which are properties of the new object
   this.sum = function() {   
       return score1 + score2;
   }
   this.getScore1 = function() {
       return score1;
   }
   this.getScore2 = function() {
       return score2;
   }
}
var randomScore = new RandomScore();
console.log(randomScore.getScore1() + '+' + randomScore.getScore2() + '=' + randomScore.sum());

The following would also work without making an object, although it would be rather unusual in practice (JSFiddle):

var getScore1, getScore2, sum;   // define variables with global scope
function randomScore() {
    var score1 = Math.random() * 10,
        score2 = Math.random() * 10;
    getScore1 = function() {
        return score1;
    }
    getScore2 = function() {
        return score2;
    }
    return score1 + score2;
}

// we need to run the function once before 
// getScore1 and getScore2 will have any functions assigned to them
sum = randomScore();

// now we can access getScore1 and getScore2 outside of our randomScore function
console.log(getScore1() + '+' + getScore2() + '=' + sum);
Stuart
  • 9,597
  • 1
  • 21
  • 30