In this function neither points nor userPoints is defined and so the code will use the extremely poor coding practice of looking for global variables with those names. In the case of userPoints there is a global variable for it to use but with points there isn’t one as the only place that variable name exists is inside of the other function.
Both of those functions are defined poorly as they rely on global variables - in fact that the code uses global variables at all is poor practice.
The whole piece of code appears to be full of errors of various sorts. I assume that the corresponding question is asking you to fix all of the errors such as the ones in the following lines:
var level = points / pointsPerLevel;
} else if (level == 1) {
as well as getting rid of the unnecessary semi colon and fixing that variable name error.
var avatar = "generic";
var skill = 1.0;
var pointsPerLevel = 1000;
var userPoints = 2008;
function getAvatar(points){
var level = points / pointsPerLevel;
if (level == 1) {
return "Teddy bear";
} else if (level == 1) {
return "Cat";
} else if (level >= 2) {
return "Gorilla";
}
};
function updatePoints(bonus, newPoints) {
var i = 0;
while(i<bonus){
newPoints = newPoints + skill * bonus;
i = i + 1;
}
return newPoints + userPoints;
}
userPoints = updatePoints(2,100);
avatar = getAvatar(2112);
It’s not an exercise, so nothing to do, but if i want to refactor it not to use global variables, should I do:
function getAvatar(points){
var pointsPerLevel = 1000;
var level = points / pointsPerLevel;
if (level == 1) {
return "Teddy bear";
} else if (level == 1) {
return "Cat";
} else if (level >= 2) {
return "Gorilla";
}
};
function updatePoints(bonus, newPoints) {
var i = 0;
var skill = 1.0;
var userPoints = 2008;
while(i<bonus){
newPoints = newPoints + skill * bonus;
i = i + 1;
}
return newPoints + userPoints;
}
userPoints = updatePoints(2,100);
var avatar = getAvatar(2112);
But in the first example, the code prints Gorilla. In the second one, it prints 2112. I sort of understand where to start looking, why but not completely.
Not sure what to do about the last two statements.
And what about this…it gives me undefined.
My version with locals - but gives me undefined…
function getAvatar(points){
var pointsPerLevel = 1000;
var level = points / pointsPerLevel;
if (level == 1) {
return “Teddy bear”;
} else if (level == 1) {
return “Cat”;
} else if (level >= 2) {
return “Gorilla”;
}
};
function updatePoints(bonus, newPoints) {
var i = 0;
var skill = 1.0;
var userPoints = 2008;
while(i<bonus){
newPoints = newPoints + skill * bonus;
i = i + 1;
}
return newPoints + userPoints;
}
function init(){
userPoints = updatePoints(2,100);
var avatar = getAvatar(2112);}
var avatar = "generic";
var skill = 1.0;
var pointsPerLevel = 1000;
var userPoints = 2008;
function getAvatar(points) {
var level = Math.floor(points / pointsPerLevel);
if (level == 1) {
return "Teddy bear";
} else if (level == 2) {
return "Cat";
} else if (level >= 3) {
return "Gorilla";
};
}
function updatePoints(points, bonus, newPoints) {
var i = 0;
while(i<points) {
newPoints = newPoints + skill * bonus;
i = i + 1;
};
return newPoints + userPoints;
}
userPoints = updatePoints(300, 2, 100);
avatar = getAvatar(userPoints);
alert(userPoints+", "+avatar);
This is from the code in the initial post. This has all been mentioned before, I’m just posting because, and please don’t get me wrong, I guess you’re a bit confused. In the getAvatar function, you have two conditions that said the same, and were mutually exclusive, when comparing the level variable. Regarding the points variable, it was not defined, so the while statement couldn’t work with it. You have two options, use a global variable, or pass it as an argument to the funtion that updates the user points. In the code above, the points variable is passed as an argument to the updatePoints function. This would be the best practice. In a real world situation you’d have several points methods, and you could just call the updatePoints function with different arguments. However, the way that function is written doesn’t make much sense to me, but if it’s only an exercise to see how the functions and variables interact, I guess it’s fine. One could say I’m missing the point of the updatePoints function.