logo

Cat years, Dog years

I have a cat and a dog. I got them at the same time as kitten/puppy. That was humanYears years ago. Return their respective ages now as [humanYears,catYears,dogYears]

Cat Years

Dog Years


My code:


                        var humanYearsCatYearsDogYears = function(humanYears) {
                            let dog = 0;
                            let cat = 0;
                            for (let i = 1; i <= humanYears; i++)
                              if (i === 1){
                                dog += 15;
                                cat += 15;
                              }else if (i === 2){
                                dog += 9;
                                cat += 9;
                              }else{
                                dog += 5;
                                cat += 4;
                              }
                            return [humanYears,cat,dog];
                          }                           
                    


And that's the best solution among all users:


                        var humanYearsCatYearsDogYears = function(y) {
                            if (y == 1) return [1, 15, 15]
                            if (y == 2) return [2, 24, 24]
                            return [y, (y-2) * 4 + 24, (y-2) * 5 + 24]
                          }