code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

024 Century From Year

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 
Given a year, return the century it is in. The first century spans from the year 1 up to and including the year 100, the second - from the year 101 up to and including the year 200, etc.

Example

For year = 1905, the output should be centuryFromYear(year) = 20;
For year = 1700, the output should be centuryFromYear(year) = 17.
*/

function centuryFromYear(value) {
  const cent = value % 100 === 0? Math.floor(value/100): Math.floor(value/100) + 1
  return cent
}

test('century From Year', () => {
  expect(centuryFromYear(1905)).toEqual(20)
  expect(centuryFromYear(1700)).toEqual(17)
});

Created 2019-12-09T03:15:46+05:18 · Edit