Hello everybody,
I’m a complete beginner learning JavaScript, I use "freecodecamp " resources and recently decided to try a book as well. having a hard time with the “function return statement”. sometimes I see “return statement " come inside the “if statement” and other times it comes outside of the” if statement " can anyone clear this up for me, please? help appreciated. here are two examples of what I’m trying to say:
Example 1:
return is inside the if statement.
function getAvatar (points) {
var level = points / pointsPerLevel;
if (level == 0) {
return “Teddy bear”;
} else if (level == 1) {
return “Cat”;
} else if (level >= 2) {
return “Gorilla”;
}
Example 2:
return is outside the if statement :
var points = 0;
function playTurn(player, location) {
points = 0;
if (location == 1) {
points = points + 100;
}
return points;
}
var total = playTurn(“Jai”, 1);
alert(points);
Thanks in advance. I did not write this code it was copied from the book that I’m currently working on.