Syntax error Input year and find in which century it falls without using slice() in JavaScript

Input year and find in which century it falls without using slice() in JavaScript



Problem

We are required to write a JavaScript function that takes in a number that represents a year and find the century that year falls in.

For instance,

1864 falls in the 19th century.

2021 falls in the 21st century.

Example

Following is the code −

 Live Demo

const year = 1956;
const findCentury = (year) => {
   let century = 0;
   for(let i = 0; i < year; i++){
      if(i % 100 === 0){
         century++;
      };
   };
   return century;
};
console.log(findCentury(year));

Output

Following is the console output −

20
Updated on: 2021-04-19T08:10:04+05:30

292 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements