Dog Years code review (check logic)

Hello, I would really appreciate if anyone could read through my code for the Dog Years challenge, and maybe help me with my logic… Whilst I understand the syntax well enough to get through the project, I don’t quite understand why there are two separate variables to calculate early/later years!

//A variable that can change value(because we are calculating our age in dog years) initialised as my age
let myAge = 26;
//the aging process takes diferent amounts of time so this is another variable explaining dog years? idk probably not getting this concept correct. 
let earlyYears = 2;
earlyYears *= 10.5;
//the variable below accounts for the first two years, which will result in an accurate reading for the rest of my lived years. 
let laterYears = myAge - 2;
//the assignment operator below ensures that the calculations are correct to figure out my age in dog years!
laterYears *= 4;
//below, the two ages are added to give the total age in dog years, yikeroonies i'm old.
let myAgeInDogYears = earlyYears + laterYears;
let myName = 'Callum'.toLowerCase();
console.log(`My name is ${myName}. I am ${myAge} years old in human years which is ${myAgeInDogYears} years old in dog years.`);

Thanks in advance, C :upside_down_face:

Hey Callum! How are you doing?

So, to address your doubt:

I don’t quite understand why there are two separate variables to calculate early/later years!

The DogYears project is an exercise that is part of the first chapter of the course Introduction to JS, in that chapter we are introduced to Variables.
So the projects at the end have the scope of make us practice on the use of variables in JS. Probably we could have declared less variables, but the scope of this exercise in particular was to declare many to practice.

When I did that exercise I opted to divide the several logical section of the program in this way:

//storing human age and human name const myAge = 20; const myName = "Giorgio".toLowerCase(); // first 2 years of dog age in human account let earlyYears = 2; //first iteration to convert dog age into dog account earlyYears *=10.5; let laterYears = myAge-2; //second iteration to convert dog age into dog account laterYears *=4; //third iteration to convert dog age into dog account const myAgeInDogYears = earlyYears + laterYears; //result console.log(`My name is ${myName} and in this experiment I am going to calculate my age in dog years.\nMy human age is:\r${myAge}\nMy first 2 two human years in dog age are:\t${earlyYears}\nThe rest of my human years in dog age are:\t${laterYears}\nFinally my current age in dog years is:\t\t${myAgeInDogYears}`);

Try to read through my comments, see if they make some sense to you :slight_smile:

Note that the exercise states:

  • The first two years of a dog’s life count as 10.5 dog years each.
  • Each year following equates to 4 dog years.

So this should address the logic of why we would separate the count with two variables, early-years and later-years.

Hopefully this helps :slight_smile:

Thank you! The final interpolated string actually helps put this in to context for me! :slight_smile:

1 Like