DEV Community

David
David

Posted on

How I Solved the CodeWars "Cat and Dog Years to Human Years" Problem in JS

I recently tackled a fun challenge that involves converting cat and dog years into human years. The problem is simple but presents a unique way of handling calculations with conditions based on age ranges.

Here’s the breakdown of how I approached and solved this challenge.

Problem Breakdown
We are provided with two numbers:

  • catYears: Represents the age of a cat in cat years.

  • dogYears: Represents the age of a dog in dog years.
    Our task is to convert these years into "human years" based on the following rules:

  1. The first two years of a cat's or dog's life are special:
  • The first year counts as 15 human years.

  • The second year counts as 9 human years.

  1. After the first two years, each additional year counts as:
  • 4 human years for cats.

  • 5 human years for dogs.

Solution Approach
I used a combination of if and else if statements to handle the different cases:

  • If the animal's age is below 15 years, it translates to 0 human years.

  • If it is between 15 and 24, that means the animal has lived one human year.

  • If the animal has surpassed 24 years, we calculate how many years are left after those first 24 years and then convert those remaining years into human years (4 per year for cats and 5 per year for dogs).

JavaScript Code
Here’s the solution I came up with:

var ownedCatAndDog = function(catYears, dogYears) {
  let humanCatYears;

  // Converting cat years to human years
  if (catYears < 15) {
    humanCatYears = 0;
  } else if (catYears < 24) {
    humanCatYears = 1;
  } else {
    humanCatYears = 2 + Math.floor((catYears - 24) / 4);
  }

  let humanDogYears;

  // Converting dog years to human years
  if (dogYears < 15) {
    humanDogYears = 0;
  } else if (dogYears < 24) {
    humanDogYears = 1;
  } else {
    humanDogYears = 2 + Math.floor((dogYears - 24) / 5);
  }

  return [humanCatYears, humanDogYears];
}
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Walkthrough

  1. Initialize Variables:
    I started by creating two variables: humanCatYears and humanDogYears. These will hold the equivalent human ages for the cat and dog.

  2. Handle Cat Years:
    For the cat:

  • If its age is less than 15 years (the equivalent of the first human year), it’s considered to be 0 human years.

  • If its age is between 15 and 24 (representing the second human year), it’s considered 1 human year.

  • If its age is more than 24, then it’s calculated as 2 + (remaining years) / 4, where the 2 accounts for the first two years, and each additional year counts as 4 human years.

  1. Handle Dog Years: Similarly for the dog:
  • If the dog's age is below 15 years, it’s 0 human years.

  • If it is between 15 and 24, that’s 1 human year.

  • If it’s above 24, we compute 2 + (remaining years) / 5, where the 2 is for the first two years, and each additional year is 5 human years.

  1. Return the Result: Finally, I returned an array containing the human equivalents for both the cat and the dog.

Top comments (0)