首页 > 解决方案 > JavaScript 应用条件和函数

问题描述

我必须根据 javascript 中的以下规则创建 IF 条件。如果可以请帮助:

  1. 主要数字之和应等于限制范围内其他数字之和

//First example
    var a = 5;  //asume a is sum of main numbers
    var b = 10; // b is sum of other numbers

    var limit = 11;  //limit
      
    if ((a==b) < limit)  //something like this
          alert("true"); 
    else
         alert("false");
         
    //It should return false as a is not equal b but within limit but it is not working.
    
    //Second example
         
    var a = 15; 
    var b = 17; 
    var limit = 11;
    
    
    if ((a==b) < limit) 
          alert("true"); 
    else
         alert("false");
    
    //third example
    var a = 15; 
    var b = 17; 
    var limit = 11;
    
    
    if ((a==b) < limit) 
          alert("true"); 
    else
         alert("false");

如何在 JavaScript 中创建 if 条件,该条件将根据上述规则返回布尔值 true 或 false。我可以在这里使用一些内置的数学函数吗,比如 Math.abs 等。

标签: javascriptfunctionmathbooleanoperators

解决方案


可能想要的是这样的:

if ((a===b) && a < limit)

更新:正如评论中指出的那样, ifa== ba < limit,b必须是< limit,所以不需要检查。


推荐阅读