Reputation: 1
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());
Upvotes: 0
Views: 47
Reputation: 9858
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);
Upvotes: 1
Reputation: 101730
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);
Upvotes: 2
Reputation: 99
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...
Upvotes: 1