Jump to content


Photo

Local Variables


  • Please log in to reply
No replies to this topic

#1 assembly21

assembly21

    New Member

  • Members
  • Pip
  • 2 posts

Posted 20 July 2015 - 06:48 AM

The book insists on local variables, however they use plenty of global ones. 

I'd like to have seen for example this piece of code written without global variables at all. 
 

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);

I try this, but it's not finished...

 

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);

Would love to see this version finished. Thanks in advance. 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users