 # Dog Years

Hello everybody,

I just did the “Dog Years” Project and i figured out it doesn’t calculate the dog years properly, if myAge = 1. (The result would be 17 years, which would be wrong - it should be 10.5).
So I fixed it with the following code:

``````if (myAge < 2) {
myAge *= 10.5;
console.log(`My age in dog years is \${myAge}`);

} else {
console.log(`My name is \${myName}. I am \${myAge} years old in human years, which is \${myAgeInDogYears} years old in dog years.`);
}
``````

Is there a better way of tackling this issue?

PS: This is my whole code:

``````/* Dogs mature at a faster rate than human beings. We often say a dog’s age can be calculated in “dog years” to account for their growth compared to a human of the same age. In some ways we could say, time moves quickly for dogs — 8 years in a human’s life equates to 45 years in a dog’s life. How old would you be if you were a dog? Here’s how you convert your age from “human years” to “dog years”:

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

let myAge = 1;
/* Setting a variable with my age. I've changed that to "let" instead of "const", so i can work with my "if/else" fix */

let earlyYears = 2;
/* Early years of a dog */

earlyYears *= 10.5;

let laterYears = myAge - 2;
/* Since we already accounted for the first two years, take the myAge variable, and subtract 2 from it.*/

laterYears *= 4;
/* Multiply the laterYears variable by 4 to calculate the number of dog years accounted for by your later years. */

myAgeInDogYears = laterYears + earlyYears;

const myName = "Dimi".toLowerCase();

/* This was the original code that I wrote initially according to the instructions:
console.log(`My name is \${myName}. I am \${myAge} years old in human years, which is \${myAgeInDogYears} years old in dog years.`); */

if (myAge < 2) {
myAge *= 10.5;
console.log(`My age in dog years is \${myAge}`);
} else {
console.log(`My name is \${myName}. I am \${myAge} years old in human years, which is \${myAgeInDogYears} years old in dog years.`);
}
``````
2 Likes

oooo this is interesting

I’m thinking we could use any source online like this:

Plot it on google sheets (or Excel), get a best fitting curve, and use the equation to calculate the dog to human age conversion at any given age! For example, for the “small” category, according to this chart, it seems like:

chart before 2 dog years shows polynomial trend: `humanAge = 16*dogAge - dogAge^3`
chart after 2 years shows linear trend: `humanAge = 4*dogAge + 16`

we could do similar trends for the other dog sizes, and have a complete solution 2 Likes

Yeah, you could

and have different scripts for the different sizes.

If you wanted to change this you could just change the numbers right? So if you wanted to calculate the dog years for an 100 year old human, you would just change the numbers?

We would have different equations based on dog size to translate dog years to human years, so we could easy do some control flow and use conditional statements to check what kind of dog we are dealing with, and that would determine which equation we use!

1 Like