Converting humanYears into catYears and dogYears in JavaScript


Problem

We are required to write a JavaScript function that takes in human age in years and returns respective dogYears and catYears.

Input

const humanYears = 15;

Output

const output = [ 15, 76, 89 ];

Example

Following is the code −

 Live Demo

const humanYears = 15;
const humanYearsCatYearsDogYears = (humanYears) => {
   let catYears = 0;
   let dogYears = 0;
   for (let i = 1; i <= humanYears; i++) {
      if (i === 1) {
         catYears += 15;
         dogYears += 15;
      }else if (i === 2) {
         catYears += 9;
         dogYears += 9;
      }else {
         catYears += 4;
         dogYears += 5;
      }
   }
   return [humanYears, catYears, dogYears];
};
console.log(humanYearsCatYearsDogYears(humanYears));

Output

[ 15, 76, 89 ]

Updated on: 17-Apr-2021

209 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements