Error with the return of a function to catch century of a year [closed]

-2

In a function in JS where the user spends the year as a parameter and the function returns the century of that year, but my function always returns 1.

function centuryFromYear(year) {
    if (year => 1 || year <= 101)return 1
    else if (year => 102 || year <= 201)return 2
    else if (year => 202 || year <= 301) return 3

    else if (year => 302 || year <= 401)return 4
    else if (year => 402 || year <= 501)return 5
    else if (year => 502 || year <= 601)return 6
    else if (year => 602 || year <= 701)return 7

    else if (year => 702 || year <= 801)return 8
    else if (year => 802 || year <= 901)return 9

    else if (year => 902 || year <= 1001)return 10
    else if (year => 1002 || year <= 1101)return 11
    else if (year => 1102 || year <= 1201)return 12

    else if (year => 1202 || year <= 1301)return 13
    else if (year => 1302 || year <= 1401)return 14
    else if (year => 1402 || year <= 1501)return 15
    else if (year => 1502 || year <= 1601)return 16
    else if (year => 1602 || year <= 1701)return 17
    else if (year => 1702 || year <= 1801)return 18
    else if (year => 1802 || year <= 1901)return 19
    else if (year => 1902 || year <= 2001)return 20
    else if (year => 2002 || year <= 2101)return 21

}
    
asked by anonymous 05.01.2019 / 14:04

4 answers

6

Pure math can be a lot simpler.

Actually it could be simpler still. I'm pretty sure that your original algorithm is wrong and so to reproduce the same results you have to make some% s of% s that should be unnecessary and a subtraction that also does not look correct but I did to give the same result. If you want me to do the right thing, just talk.

function centuryFromYear(year) {
    if (year == 1) return 1;
    if (year > 2101 || year < 1) return null;
    return Math.trunc((year - 2) / 100) + 1;
}
console.log(centuryFromYear(-1));
console.log(centuryFromYear(0));
console.log(centuryFromYear(1));
console.log(centuryFromYear(50));
console.log(centuryFromYear(100));
console.log(centuryFromYear(101));
console.log(centuryFromYear(102));
console.log(centuryFromYear(150));
console.log(centuryFromYear(2000));
console.log(centuryFromYear(2100));
console.log(centuryFromYear(2200));
    
05.01.2019 / 14:59
2

In your condition you are putting || instead of && . Think well, if the user "sends" any year above 1 it will always fall on the first IF because the first condition will satisfy the function.

  

if (year >= 1 || year <= 101)return 1

Remember that the OU logical operator only needs one of the conditions to be true to enter IF .

In this case, you only need to change your operator || to && , where it will be checked if the year entered is between the period you want.

function centuryFromYear(year) {
    if (year >= 1 && year <= 101)return 1
    else if (year >= 102 && year <= 201)return 2
    else if (year >= 202 && year <= 301) return 3

    else if (year >= 302 && year <= 401)return 4
    else if (year >= 402 && year <= 501)return 5
    else if (year >= 502 && year <= 601)return 6
    else if (year >= 602 && year <= 701)return 7

    else if (year >= 702 && year <= 801)return 8
    else if (year >= 802 && year <= 901)return 9

    else if (year >= 902 && year <= 1001)return 10
    else if (year >= 1002 && year <= 1101)return 11
    else if (year >= 1102 && year <= 1201)return 12

    else if (year >= 1202 && year <= 1301)return 13
    else if (year >= 1302 && year <= 1401)return 14
    else if (year >= 1402 && year <= 1501)return 15
    else if (year >= 1502 && year <= 1601)return 16
    else if (year >= 1602 && year <= 1701)return 17
    else if (year >= 1702 && year <= 1801)return 18
    else if (year >= 1802 && year <= 1901)return 19
    else if (year >= 1902 && year <= 2001)return 20
    else if (year >= 2002 && year <= 2101)return 21
}

Useful links: Logical Operators

I hope I have helped.

    
05.01.2019 / 14:52
2

As already mentioned, the main problem is in the || operator. But you can still use a much more minimized function with ternary operators :

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

console.log("Ano 85: séc. ", centuryFromYear(85));
console.log("Ano 101: séc. ", centuryFromYear(101));
console.log("Ano 1500: séc. ", centuryFromYear(1500));
console.log("Ano 1501: séc. ", centuryFromYear(1501));
console.log("Ano 1999: séc. ", centuryFromYear(1999));
console.log("Ano 2019: séc. ", centuryFromYear(2019));
console.log("Ano 2101: séc. ", centuryFromYear(2101));
    
05.01.2019 / 22:13
0

The way it is always going to give 1 because in the first line (of if ) it checks if it is greater than or equal to 1 and any last number will always be greater than or equal to 1

I suggest a much more elegant function:

function centuryFromYear(year) {
    var newYear = year.toString().length;
    var stYear
    if (newYear >= 4){
        stYear = year.toString().substring(0, 2);
        if (year % 100 !== 0){ 
        return (parseInt(stYear)+1);
        } else {
        return parseInt(stYear);  
        };
    } else if (newYear >= 3){
        stYear = year.toString().substring(0,1);
        if (year % 100 !== 0){ 
        return (parseInt(stYear)+1);
        } else {
        return parseInt(stYear);  
        };
    } else if (newYear < 3){
        return 1;
    };
};

console.log(centuryFromYear(902));

font

    
05.01.2019 / 15:00