-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
– Emerson Vieira
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.
– LeAndrade
Besides, you have to replace
||
for&&
. But the correct is to refactor the code. You can find the result with a division– Valdeir Psr
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.– Bacco