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

[Codewars] Century From Year (8 kyu) / JavaScript

by fluss 2022. 12. 24.

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

 

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:

Introduction

The first century spans from the year 1 up to and including the year 100, the second century - from the year 101 up to and including the year 200, etc.

 

Task

Given a year, return the century it is in.

 

Examples

1705 --> 18
1900 --> 19
1601 --> 17
2000 --> 20
 

소개

The first century spans from the year 1 up to and including the year 100, the second century - from the year 101 up to and including the year 200, etc.

1세기는 1년부터 100년까지, 2세기는 101년부터 200까지, 등등입니다.

 

작업

Given a year, return the century it is in.

연도가 주어지면 세기를 반환하세요

 

예시

1705 --> 18
1900 --> 19
1601 --> 17
2000 --> 20

 

풀이

주어진 연도를 100으로 나눈 몫을 반환하는데 주어진 연도를 100으로 나눈 나머지가 0보다 크다면 1을 더해서 반환해 주었다.

 

코드

function century(year) {
  let cen = parseInt(year / 100);
  return year % 100 > 0 ? cen + 1 : cen;
}

댓글