简体   繁体   English

javascript如果数字可以被整除,则为true;否则为true

[英]javascript if number is evenly divisible true if not false

I have to write Java script that can read two integers from the user, if the first number the user entered is evenly divisible by the second number, then it will display an alert of TRUE to the user, and FALSE otherwise. 我必须编写一个Java脚本,该脚本可以从用户读取两个整数,如果用户输入的第一个数字可以被第二个数字均分,那么它将向用户显示TRUE警报,否则显示FALSE。 If either number the user enters is ZERO the alert should display FALSE to the user. 如果用户输入的任何一个数字为零,则警报应向用户显示FALSE。

this is my code it does not work 这是我的代码,它不起作用

        var y = prompt("Enter a Value","");
      var z = prompt("Enter a Value","");

      if  (y % z === 0) {
      greeting = "TRUE";
      } else (y % z !== 0 ||
      {
      greeting = "FALSE"
      document. get Element By Id ("true false").inner HTML = greeting;

If either number the user enters is ZERO the alert should display FALSE to the user. 如果用户输入的任何一个数字为零,则警报应向用户显示FALSE。

change you if condition to 如果条件更改为

if  (x && y && y % z === 0) {

Rest of the code is 其余的代码是

if  (x && y && y % z === 0) 
{
   greeting = "TRUE";
}
else
{
   greeting = "FALSE";
}

如果您不熟悉javascript,这里是使用三元运算符实现此非常常见模式的另一种方法:

var greeting = (x && y && y % z === 0) ? "TRUE" : "FALSE";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM