Here is another javascript project after Kelvin weather, the calculation of Dog years in javascript.
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.
By using JavaScript here is how to convert your human age into dog years.
// My age which is counted as human years. const myAge = 28; // The first 2 years of dog. let earlyYears = 2; // The calculation of early 2 years of dog. earlyYears *= 10.5; // The later years of dog. let laterYears = myAge - 2; // The calculation of later years of dog. laterYears *= 4; // My name and that too in lowercase. const myName = "Atul".toLowerCase(); // Summing the dog years combining their early and later years. const myAgeInDogYears = earlyYears + laterYears; // Outcome. console.log(`My name is ${myName}. I am ${myAge} years old in human years which is ${myAgeInDogYears} years old in dog year.`);
In the last line, I have used the backtick not the apostrophe symbol, do remember that.
The outcome of the above code should be:
My name is atul. I am 28 years old in human years which is 125 years old in dog year.
That’s how we have completed another javascript project for calculating a dog’s age.
Leave a Reply