Error with the return of a function to catch a century of a year

Asked

Viewed 366 times

-2

In a function in JS where the user spends the year as parameter and the function returns which century of this year, however 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

}
  • your code is very confusing, I suggest you refactor and based on this function of the following link: https://gist.github.com/dillansimmons/6e4b0cf88b99cc3a9149eebd1ded349a

  • 2

    Face the comparison is wrong, is not => for greater or equal this is a Arrow Function and has another feature. The greater or equal sign is this >= javascript.

  • 1

    Besides, you have to replace || for &&. But the correct is to refactor the code. You can find the result with a division

  • 1

    First of all, it is basic syntax error in =>, as already stated by fellow @Leandrade. This here is not a comparison symbol: =>, that is to say: >= - Even so, any value will meet the condition (year >= 1 || year <= 101). "Greater than a OR less than 101". If you want to restrict to a track, you would have to say (year >= 1 && year <= 101) "Greater than a And less than 101". Both true.

4 answers

6


Pure mathematics can be much simpler.

Actually, it could be simpler. I’m almost sure your original algorithm is wrong and so to reproduce the same results have to do some ifIt’s supposed to be unnecessary and a subtraction that doesn’t seem right either, but I did it to give the same result. If you wish me to do the right thing, just say the word.

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));

I put in the Github for future reference.

2

In your condition you’re putting || instead of &&. Think about it, if the user "submit" any year above 1 it will always fall in the first IF because the first condition will satisfy the function.

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

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

In this case, you just need to change your operator || for &&, where it will be verified whether the year reported is amid 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’ve helped.

2

As you said, the main problem is 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));

0

The way it always is 1 because right in the first line (from if) checks whether it is greater than or equal to 1 and any number past will always be greater 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));

source

Browser other questions tagged

You are not signed in. Login or sign up in order to post.