首页 > 解决方案 > 使用 if-else 降序而不使用逻辑运算符 Javascript

问题描述

给定三个数字,使用上述if...else语句按降序打印它们。不允许使用任何逻辑运算符来解决此问题,但欢迎使用比较运算符。

    var a = 3;
    var b = 2;
    var c = 1;

    if(a<b<c){
      console.log(c,b,a)
    }
    if(b<c<a){
      console.log(b,c,a)
    }
    if(c<a<b){
      console.log(c,a,b)
    }
// I understand this is wrong as when I do a<b<c
// First it compares a<b = true(1) or false(0) and remaining compares to c
// the answer in boolean 
// How do I approach this problem?

标签: javascriptif-statement

解决方案


    var a = 5
    var b = 2
    var c = 3

    if(a<b){
      if(c<a){
        console.log(b,a,c);
      } else if(b<c){
        console.log(c,b,a);
    }else{
        console.log(b,c,a);
      } 
      } else if(a<c){
        console.log(c,a,b);
      } else if(c<b){
        console.log(a,b,c);
      }

    else{
        console.log(a,c,b);
      }

//了解Bubble、Insertion排序的概念更好理解。


推荐阅读