 # I've just revisited the "My Age In Dog Years" project and I'm having some issues with my code

Hi! I’ve been making some progress on the javascript course and decided to revisit one of my earlier exercises, “my age in dog years”.

https://www.codecademy.com/courses/introduction-to-javascript/projects/dog-years-javascript

My code looks like this:

const myAge = 1;
let earlyYears = 2;
earlyYears *= 10.5;
let laterYears = myAge - 2;
laterYears *= 4;
let myAgeInDogYears = earlyYears + laterYears;
let myName = ‘n’.toLowerCase();
console.log(`My name is \${myName}. I am \${myAge} years old in human years which is \${myAgeInDogYears} years old in dog years.`);

The console logs:

My name is n. I am 1 years old in human years which is 17 years old in dog years.

My understanding is that if my age was 1, the console should be logging 10.5. I’m not sure where it’s getting 17 from, if anyone could explain it it would be greatly appreciated!

Edit: Just read through the code again, I understand where 17 is coming from (earlyYears = 21 and laterYears = -4). I guess this exercise just isn’t meant to work for one years olds?

Having worked that out, I now no longer understand why we use

let earlyYears = 2;
earlyYears *= 10.5;

as opposed to just writing

let earlyYears = 21

The calculation for `myAgeInDogYears` is `earlyYears + laterYears`. When `myAge = 1`, then `earlyYears = 21` regardless as there is no conditional preventing the calculation from taking place. then `laterYears = myAge - 2 * 4`, which would be `-1 * 4 = -4`. So we end up having `myAgeInDogYears = 21 - 4 = 17`. You need to have a conditional around your age calculation and `earlyYears` calculation so that it only executes the full scope of the current code when the age is larger than 2. I would do something like below:

``````const myAge = 1;
let myAgeInDogYears;

if (myAge >= 2) {
let earlyYears = 2;
earlyYears *= 10.5;
let laterYears = myAge - 2;
laterYears *= 4;
myAgeInDogYears = earlyYears + laterYears;
} else if (myAge >= 0){
myAgeInDogYears = myAge * 10.5
}

let myName = 'n'.toLowerCase();
console.log(`My name is \${myName}. I am \${myAge} years old in human years which is \${myAgeInDogYears} years old in dog years.`);
``````

Got it, thank you! Is there a reason for:

``````let earlyYears = 2;
earlyYears *= 10.5
``````

as opposed to

``````let earlyYears = 21
``````

In this case not particularly, since outside of being less than 2 `earlyYears` is effectively a constant, however you could also write the program in such a way that `earlyYears` gives you the age of less than 2 also, and thus it would come in handy to have it like this. I structured it based on your code to make it easy to see the differences, but the program can always be optimised!

1 Like