본문 바로가기
알고리즘/Codewars

[Codewars] Cat years, Dog years (8 kyu) / JavaScript

by fluss 2023. 4. 12.

https://www.codewars.com/kata/5a6663e9fd56cb5ab800008b

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

Kata Task

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]

NOTES:

  • humanYears >= 1
  • humanYears are whole numbers only

 

Cat Years

  • 15 cat years for first year
  • +9 cat years for second year
  • +4 cat years for each year after that

 

Dog Years

  • 15 dog years for first year
  • +9 dog years for second year
  • +5 dog years for each year after that

References


If you liked this Kata there is another related one here

 

코드

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

댓글