Js switch's strange question

var result_time = 45852221;
var simple_text;

switch (result_time) {

    case result_time < 60:

        simple_text =  "";

        break;


    case  result_time > 60 && result_time < 3600 : //

        simple_text = Math.round(result_time/60)+"";


        break;


    case  result_time > 3600 && result_time < 86400 : //

        simple_text = Math.round(result_time/60/24)+"";

        break;

    case  result_time > 86400 && result_time < 2592000 : //

        simple_text = Math.round(result_time/60/60/24)+"";


        break;

    case  result_time > 2592000 && result_time < 31104000 : //

        simple_text = Math.round(result_time/60/60/24/30)+"";


        break;


    case  result_time > 31104000: //

        simple_text = Math.round(result_time/60/60/24/30/12)+"";


        break;
}
document.write(simple_text)

as shown in the figure above, this is a judgment statement that simplifies time. Why can"t it judge any case??

Mar.20,2021

just change switch (result_time) to switch (true) .

switch usage:

switch (expression)
  case value:
    statement;
    break;
  case value:
    statement;
    break;

expression and value can be values or expressions (expressions are actually values, 1 < 2 is equivalent to true ),
as long as expression equals value , the related statement

will be executed.

for example:

switch (1 < 2)
{
    case true:
        console.log('true');
        break;
    case false :
        console.log('false');
        break;
}
switch (true)
{
    case 1 < 2:
        console.log('true');
        break;
    case 1 > 2 :
        console.log('false');
        break;
}

the above two codes are actually the same


because switch-case is not used at all, the values in case are values rather than Boolean expressions. So each of your case is a Boolean value: true or false , while result_time is neither true nor false , of course you can't tell. You can only use if-else , or write expressions manually according to specific rules.


Don't you search the document before the subject comes to ask questions?

MDN switch-case

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3e6a9-2c3f4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3e6a9-2c3f4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?