Getting century from year

So, I have a problem where I have to return the century. I have made a code but it fails for years like 1700 or 1800. My question is, how can I make a special case that if the year is fixed, like 2000 it only divides the year by 100? I have tried with

element.slice(-2);

but it didn't work. Here's my current code

function centuryFromYear(year) {
    var x = Math.floor(year/100) + 1;
    return x; 
}

Upvotes: 8

Views: 28882

Answers (10)

jose_sandele
jose_sandele

Reputation: 1

//The code work fine

function solution(year) {
    
    let result = year / 100 + 1;
    
    console.log(Math.floor(result));
}

solution(1905);//20

Upvotes: 0

Ian Odhiambo
Ian Odhiambo

Reputation: 21

function centuryFromYear(year) {
 return Math.ceil(year / 100)
}

Upvotes: 0

Matt
Matt

Reputation: 20796

Subtract 1 from year first:

function centuryFromYear(year) {
    return Math.floor((year-1)/100) + 1;
}

Examples:

centuryFromYear(1999) // 20
centuryFromYear(2000) // 20
centuryFromYear(2001) // 21

Better yet, use Math.ceil(year/100) as in Yakir's answer.

Upvotes: 12

Nhu Phuong Anh
Nhu Phuong Anh

Reputation: 11

function centuryFromYear(year) {
var testInt = Number.isInteger(year/100);
    if (year >= 1 && testInt == true ) {
        return Math.floor(year/100);
    }
    else if ( year >= 1 && testInt == false) {
        return Math.floor(year/100) + 1;
    }

}

Upvotes: 1

Yakir Fitousi
Yakir Fitousi

Reputation: 545

you can have it shortest as:

return Math.ceil(year/100);

Upvotes: 23

Robert
Robert

Reputation: 1

First subtract a year from the year to avoid messing up every 100 year.

int centuryFromYear(int year) {
int century = Math.floor((year-1) / 100) + 1;
return century;}

Upvotes: 0

Gonzalo Arró
Gonzalo Arró

Reputation: 21

You can check for that special case just by doing something like this:

function centuryFromYear(year) {
    var x = Math.floor(year / 100);
    if (year % 100 === 0)
        return x;
    else
        return x + 1;
}

Upvotes: 2

huysentruitw
huysentruitw

Reputation: 28141

I would probably go for this:

function centuryFromYear(year) {
    return Math.floor((year + 99) / 100);
}

Upvotes: 0

Attersson
Attersson

Reputation: 4866

Last two digits:

let myNumber = 2000;
console.log(myNumber.toString().slice(-2))

First two digits:

let myNumber = 2000;
console.log(myNumber.toString().slice(0,2))

Final function:

function centuryFromYear(year) {
  if(typeof year == 'string')
    if(year.toString().slice(-2) == '00')
      return year.toString().slice(0,2);
    else
      return (Math.floor(+year/100) +1).toString();
  else if(typeof year == 'number')
    return Math.floor((year-1)/100) + 1;
  else
    return undefined;
} 
    
console.log(centuryFromYear("2000"));
console.log(centuryFromYear("1901"));
console.log(centuryFromYear("2002"));
console.log(centuryFromYear(2002));
console.log(centuryFromYear(1999));
console.log(centuryFromYear(2000));

Upvotes: 4

Roy Scheffers
Roy Scheffers

Reputation: 3908

Add an extra check in your function that checks if the year is a rounded century like 2000 or 1800. Here's how that would look.

function centuryFromYear(year) {
    var x = year % 100 === 0 ? year/100 : Math.floor(year/100) + 1;
    return x; 
}

console.log('2000 => ', centuryFromYear(2000));
console.log('2001 => ', centuryFromYear(2001));

Upvotes: 1

Related Questions