- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
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
Advertisements