Hướng dẫn check if number is even javascript - kiểm tra xem số có phải là javascript chẵn không

Tôi quyết định tạo hàm isEven và isodd đơn giản với thuật toán rất đơn giản:

function isEven(n) {
  n = Number(n);
  return n === 0 || !!(n && !(n%2));
}

function isOdd(n) {
  return isEven(Number(n) + 1);
}

Đó là OK nếu N có một số tham số nhất định, nhưng thất bại trong nhiều kịch bản. Vì vậy, tôi đã đặt ra để tạo các chức năng mạnh mẽ mang lại kết quả chính xác cho càng nhiều kịch bản càng tốt, để chỉ các số nguyên trong giới hạn của các số JavaScript được kiểm tra, mọi thứ khác trả về sai (bao gồm + và - vô cực). Lưu ý rằng số không là chẵn.

// Returns true if:
//
//    n is an integer that is evenly divisible by 2
//
// Zero (+/-0) is even
// Returns false if n is not an integer, not even or NaN
// Guard against empty string

(function (global) {

  function basicTests(n) {

    // Deal with empty string
    if (n === '') 
      return false;

    // Convert n to Number (may set to NaN)
    n = Number(n);

    // Deal with NaN
    if (isNaN(n)) 
      return false;

    // Deal with infinity - 
    if (n === Number.NEGATIVE_INFINITY || n === Number.POSITIVE_INFINITY)
      return false;

    // Return n as a number
    return n;
  }

  function isEven(n) {

    // Do basic tests
    if (basicTests(n) === false)
      return false;

    // Convert to Number and proceed
    n = Number(n);

    // Return true/false
    return n === 0 || !!(n && !(n%2));
  }
  global.isEven = isEven;

  // Returns true if n is an integer and (n+1) is even
  // Returns false if n is not an integer or (n+1) is not even
  // Empty string evaluates to zero so returns false (zero is even)
  function isOdd(n) {

    // Do basic tests
    if (basicTests(n) === false)
      return false;

    // Return true/false
    return n === 0 || !!(n && (n%2));
  }
  global.isOdd = isOdd;

}(this));

Bất cứ ai có thể thấy bất kỳ vấn đề với những điều trên? Có một phiên bản tốt hơn (nghĩa là chính xác hơn, nhanh hơn hoặc ngắn gọn hơn mà không bị che khuất)?

Có nhiều bài đăng khác nhau liên quan đến các ngôn ngữ khác, nhưng dường như tôi không thể tìm thấy một phiên bản dứt khoát cho Ecmascript.

PSST! Tạo một tài khoản DigitalOcean và nhận được 200 đô la tín dụng miễn phí cho các dịch vụ và dịch vụ dựa trên đám mây.

Bình luận

  1. Tại sao không chỉ

    hàm isEven (giá trị) {return (giá trị%2 == 0); }
    return (value%2 == 0);
    }

    • In nó cho dù nó có lẻ hay thậm chí là quan trọng không kém !!! Nhưng logic khá tốt .. ngọt ngào và đơn giản .. !!

  2. Và phiên bản PHP chỉ cần một dấu hiệu đô la thêm:

    hàm isEven ($ int) {return ($ int%2 == 0); }
    return ($int%2 == 0);
    }

    Trong những thời điểm này, tôi có thể giả sử hầu hết các webDevvers sử dụng thư viện JavaScript như jQuery hoặc tương tự. Các thư viện này có các chức năng như ISEVen tích hợp.

    Đối với những người không sử dụng các thư viện như vậy: đoạn trích tuyệt vời;)

  3. Tôi đã bắt gặp http://rindovincent.blogspot.com/p/javascript.html nơi có một chương trình JavaScript đơn giản để tìm số đó là lẻ hay chẵn. Tôi đang dán cùng một mã với sự cho phép ở đây.

    var n = nhắc nhở (Nhập một số để tìm lẻ hoặc thậm chí là, nhập số của bạn ở đây); n = ParseInt (n); if (isnan (n)) {alert (Hồi xin vui lòng nhập số số); } other if (n == 0) {alert (Số Số là 0); } if if (n%2) {alert (Số Số là lẻ); } other {alert (số Số thậm chí là người khác); }
    n = parseInt(n);
    if (isNaN(n))
    {
    alert(“Please Enter a Number”);
    }
    else if (n == 0)
    {
    alert(“The number is zero”);
    }
    else if (n%2)
    {
    alert(“The number is odd”);
    }
    else
    {
    alert(“The number is even”);
    }

    • Thaannkkkk yoouuu soooo muccchhh .. rất hữu ích

  4. hàm isEven (value) {return /^\ d+$ /. test (value.toString ())? (Giá trị%2 === 0? Đúng: Sai): Sai; };
    {
    return /^\d+$/.test(value.toString()) ? (value%2 === 0 ? true : false ) : false;
    };

  5. // Vì vậy, hàm của tôi =) hàm isEven (value) {return isfinite (+value) &&! (+value % 2)}
    function isEven(value){
    return isFinite(+value) && !(+value % 2)
    }

  6. hàm isodd (n) {return 1 & n; }
    return 1&n;
    }

    Tôi có thắng không?

    • Đó là cái tốt. Nhưng điều gì có nghĩa là 1 & n? Bạn có thể vui lòng giải thích?

    • Nó chỉ có 10 năm sau, nhưng bây giờ tôi đã vấp ngã ‘khi này

      Có PJ, đây là tốt nhất và thanh lịch nhất.

      Như Marwils đã viết, nó có một chút và hoạt động. Điều này có nghĩa là bạn chỉ kiểm tra xem bit nhất (*) có quyền nhất của byte đúng nhất của số (**) có được đặt hay không. Nếu vậy, nó là kỳ quặc, nếu không nó là chẵn.
      This means that you only check whether the most right (*) bit of the most right byte of the number (**) is set. If so, it is odd, otherwise it is even.

      Tôi có cảm giác rằng hầu hết các lập trình viên ngày nay không biết về khái niệm về một bit bit ‘, họ chỉ xử lý các đối tượng và kế thừa. Điều đó ok, không còn nghi ngờ gì nữa, nhưng đừng quên những điều cơ bản, các bit :-)

      . (**) Điều tương tự cũng xảy ra với các số> 255, họ cần nhiều hơn 1 byte.
      (**) The same goes for numbers > 255, they need more than 1 byte.

  7. Bắt đầu đọc n nhắc nhở = n/2 if (nhắc nhở = 0) in số thậm chí còn là người khác in số
    read n
    reminder=n/2
    if (reminder=0)
    print”number is even”
    else
    print”number is odd”
    stop

  8. Tôi đã đọc rằng == so sánh là nguy hiểm vì nó ép buộc loại để so sánh để nó thực hành tốt hơn để sử dụng === sẽ trả về đúng nếu cả bên trái/bên phải thực sự giống hệt nhau.

    • Nói chung, nó thực hành tốt nhất để sử dụng === nhưng trong ví dụ này, value % 2 được đảm bảo để trả về một số nguyên hoặc

      // Returns true if:
      //
      //    n is an integer that is evenly divisible by 2
      //
      // Zero (+/-0) is even
      // Returns false if n is not an integer, not even or NaN
      // Guard against empty string
      
      (function (global) {
      
        function basicTests(n) {
      
          // Deal with empty string
          if (n === '') 
            return false;
      
          // Convert n to Number (may set to NaN)
          n = Number(n);
      
          // Deal with NaN
          if (isNaN(n)) 
            return false;
      
          // Deal with infinity - 
          if (n === Number.NEGATIVE_INFINITY || n === Number.POSITIVE_INFINITY)
            return false;
      
          // Return n as a number
          return n;
        }
      
        function isEven(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Convert to Number and proceed
          n = Number(n);
      
          // Return true/false
          return n === 0 || !!(n && !(n%2));
        }
        global.isEven = isEven;
      
        // Returns true if n is an integer and (n+1) is even
        // Returns false if n is not an integer or (n+1) is not even
        // Empty string evaluates to zero so returns false (zero is even)
        function isOdd(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Return true/false
          return n === 0 || !!(n && (n%2));
        }
        global.isOdd = isOdd;
      
      }(this));
      
      0, cho phép chúng tôi hoàn toàn biết loại biến mà chúng tôi so sánh ở hai bên của
      // Returns true if:
      //
      //    n is an integer that is evenly divisible by 2
      //
      // Zero (+/-0) is even
      // Returns false if n is not an integer, not even or NaN
      // Guard against empty string
      
      (function (global) {
      
        function basicTests(n) {
      
          // Deal with empty string
          if (n === '') 
            return false;
      
          // Convert n to Number (may set to NaN)
          n = Number(n);
      
          // Deal with NaN
          if (isNaN(n)) 
            return false;
      
          // Deal with infinity - 
          if (n === Number.NEGATIVE_INFINITY || n === Number.POSITIVE_INFINITY)
            return false;
      
          // Return n as a number
          return n;
        }
      
        function isEven(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Convert to Number and proceed
          n = Number(n);
      
          // Return true/false
          return n === 0 || !!(n && !(n%2));
        }
        global.isEven = isEven;
      
        // Returns true if n is an integer and (n+1) is even
        // Returns false if n is not an integer or (n+1) is not even
        // Empty string evaluates to zero so returns false (zero is even)
        function isOdd(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Return true/false
          return n === 0 || !!(n && (n%2));
        }
        global.isOdd = isOdd;
      
      }(this));
      
      1 hoặc
      // Returns true if:
      //
      //    n is an integer that is evenly divisible by 2
      //
      // Zero (+/-0) is even
      // Returns false if n is not an integer, not even or NaN
      // Guard against empty string
      
      (function (global) {
      
        function basicTests(n) {
      
          // Deal with empty string
          if (n === '') 
            return false;
      
          // Convert n to Number (may set to NaN)
          n = Number(n);
      
          // Deal with NaN
          if (isNaN(n)) 
            return false;
      
          // Deal with infinity - 
          if (n === Number.NEGATIVE_INFINITY || n === Number.POSITIVE_INFINITY)
            return false;
      
          // Return n as a number
          return n;
        }
      
        function isEven(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Convert to Number and proceed
          n = Number(n);
      
          // Return true/false
          return n === 0 || !!(n && !(n%2));
        }
        global.isEven = isEven;
      
        // Returns true if n is an integer and (n+1) is even
        // Returns false if n is not an integer or (n+1) is not even
        // Empty string evaluates to zero so returns false (zero is even)
        function isOdd(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Return true/false
          return n === 0 || !!(n && (n%2));
        }
        global.isOdd = isOdd;
      
      }(this));
      
      2, nhưng không phải là
      // Returns true if:
      //
      //    n is an integer that is evenly divisible by 2
      //
      // Zero (+/-0) is even
      // Returns false if n is not an integer, not even or NaN
      // Guard against empty string
      
      (function (global) {
      
        function basicTests(n) {
      
          // Deal with empty string
          if (n === '') 
            return false;
      
          // Convert n to Number (may set to NaN)
          n = Number(n);
      
          // Deal with NaN
          if (isNaN(n)) 
            return false;
      
          // Deal with infinity - 
          if (n === Number.NEGATIVE_INFINITY || n === Number.POSITIVE_INFINITY)
            return false;
      
          // Return n as a number
          return n;
        }
      
        function isEven(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Convert to Number and proceed
          n = Number(n);
      
          // Return true/false
          return n === 0 || !!(n && !(n%2));
        }
        global.isEven = isEven;
      
        // Returns true if n is an integer and (n+1) is even
        // Returns false if n is not an integer or (n+1) is not even
        // Empty string evaluates to zero so returns false (zero is even)
        function isOdd(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Return true/false
          return n === 0 || !!(n && (n%2));
        }
        global.isOdd = isOdd;
      
      }(this));
      
      3.

      Vấn đề xảy ra nếu bạn vô tình nhận được

      // Returns true if:
      //
      //    n is an integer that is evenly divisible by 2
      //
      // Zero (+/-0) is even
      // Returns false if n is not an integer, not even or NaN
      // Guard against empty string
      
      (function (global) {
      
        function basicTests(n) {
      
          // Deal with empty string
          if (n === '') 
            return false;
      
          // Convert n to Number (may set to NaN)
          n = Number(n);
      
          // Deal with NaN
          if (isNaN(n)) 
            return false;
      
          // Deal with infinity - 
          if (n === Number.NEGATIVE_INFINITY || n === Number.POSITIVE_INFINITY)
            return false;
      
          // Return n as a number
          return n;
        }
      
        function isEven(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Convert to Number and proceed
          n = Number(n);
      
          // Return true/false
          return n === 0 || !!(n && !(n%2));
        }
        global.isEven = isEven;
      
        // Returns true if n is an integer and (n+1) is even
        // Returns false if n is not an integer or (n+1) is not even
        // Empty string evaluates to zero so returns false (zero is even)
        function isOdd(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Return true/false
          return n === 0 || !!(n && (n%2));
        }
        global.isOdd = isOdd;
      
      }(this));
      
      4 hoặc
      // Returns true if:
      //
      //    n is an integer that is evenly divisible by 2
      //
      // Zero (+/-0) is even
      // Returns false if n is not an integer, not even or NaN
      // Guard against empty string
      
      (function (global) {
      
        function basicTests(n) {
      
          // Deal with empty string
          if (n === '') 
            return false;
      
          // Convert n to Number (may set to NaN)
          n = Number(n);
      
          // Deal with NaN
          if (isNaN(n)) 
            return false;
      
          // Deal with infinity - 
          if (n === Number.NEGATIVE_INFINITY || n === Number.POSITIVE_INFINITY)
            return false;
      
          // Return n as a number
          return n;
        }
      
        function isEven(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Convert to Number and proceed
          n = Number(n);
      
          // Return true/false
          return n === 0 || !!(n && !(n%2));
        }
        global.isEven = isEven;
      
        // Returns true if n is an integer and (n+1) is even
        // Returns false if n is not an integer or (n+1) is not even
        // Empty string evaluates to zero so returns false (zero is even)
        function isOdd(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Return true/false
          return n === 0 || !!(n && (n%2));
        }
        global.isOdd = isOdd;
      
      }(this));
      
      5 dưới dạng đầu vào, điều này chuyển đổi một cách lỏng lẻo thành
      // Returns true if:
      //
      //    n is an integer that is evenly divisible by 2
      //
      // Zero (+/-0) is even
      // Returns false if n is not an integer, not even or NaN
      // Guard against empty string
      
      (function (global) {
      
        function basicTests(n) {
      
          // Deal with empty string
          if (n === '') 
            return false;
      
          // Convert n to Number (may set to NaN)
          n = Number(n);
      
          // Deal with NaN
          if (isNaN(n)) 
            return false;
      
          // Deal with infinity - 
          if (n === Number.NEGATIVE_INFINITY || n === Number.POSITIVE_INFINITY)
            return false;
      
          // Return n as a number
          return n;
        }
      
        function isEven(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Convert to Number and proceed
          n = Number(n);
      
          // Return true/false
          return n === 0 || !!(n && !(n%2));
        }
        global.isEven = isEven;
      
        // Returns true if n is an integer and (n+1) is even
        // Returns false if n is not an integer or (n+1) is not even
        // Empty string evaluates to zero so returns false (zero is even)
        function isOdd(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Return true/false
          return n === 0 || !!(n && (n%2));
        }
        global.isOdd = isOdd;
      
      }(this));
      
      2 hoặc
      // Returns true if:
      //
      //    n is an integer that is evenly divisible by 2
      //
      // Zero (+/-0) is even
      // Returns false if n is not an integer, not even or NaN
      // Guard against empty string
      
      (function (global) {
      
        function basicTests(n) {
      
          // Deal with empty string
          if (n === '') 
            return false;
      
          // Convert n to Number (may set to NaN)
          n = Number(n);
      
          // Deal with NaN
          if (isNaN(n)) 
            return false;
      
          // Deal with infinity - 
          if (n === Number.NEGATIVE_INFINITY || n === Number.POSITIVE_INFINITY)
            return false;
      
          // Return n as a number
          return n;
        }
      
        function isEven(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Convert to Number and proceed
          n = Number(n);
      
          // Return true/false
          return n === 0 || !!(n && !(n%2));
        }
        global.isEven = isEven;
      
        // Returns true if n is an integer and (n+1) is even
        // Returns false if n is not an integer or (n+1) is not even
        // Empty string evaluates to zero so returns false (zero is even)
        function isOdd(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Return true/false
          return n === 0 || !!(n && (n%2));
        }
        global.isOdd = isOdd;
      
      }(this));
      
      7 khi sử dụng toán tử
      // Returns true if:
      //
      //    n is an integer that is evenly divisible by 2
      //
      // Zero (+/-0) is even
      // Returns false if n is not an integer, not even or NaN
      // Guard against empty string
      
      (function (global) {
      
        function basicTests(n) {
      
          // Deal with empty string
          if (n === '') 
            return false;
      
          // Convert n to Number (may set to NaN)
          n = Number(n);
      
          // Deal with NaN
          if (isNaN(n)) 
            return false;
      
          // Deal with infinity - 
          if (n === Number.NEGATIVE_INFINITY || n === Number.POSITIVE_INFINITY)
            return false;
      
          // Return n as a number
          return n;
        }
      
        function isEven(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Convert to Number and proceed
          n = Number(n);
      
          // Return true/false
          return n === 0 || !!(n && !(n%2));
        }
        global.isEven = isEven;
      
        // Returns true if n is an integer and (n+1) is even
        // Returns false if n is not an integer or (n+1) is not even
        // Empty string evaluates to zero so returns false (zero is even)
        function isOdd(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Return true/false
          return n === 0 || !!(n && (n%2));
        }
        global.isOdd = isOdd;
      
      }(this));
      
      8. Điều này có thể gây ra một số vấn đề trong mã của bạn nếu
      // Returns true if:
      //
      //    n is an integer that is evenly divisible by 2
      //
      // Zero (+/-0) is even
      // Returns false if n is not an integer, not even or NaN
      // Guard against empty string
      
      (function (global) {
      
        function basicTests(n) {
      
          // Deal with empty string
          if (n === '') 
            return false;
      
          // Convert n to Number (may set to NaN)
          n = Number(n);
      
          // Deal with NaN
          if (isNaN(n)) 
            return false;
      
          // Deal with infinity - 
          if (n === Number.NEGATIVE_INFINITY || n === Number.POSITIVE_INFINITY)
            return false;
      
          // Return n as a number
          return n;
        }
      
        function isEven(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Convert to Number and proceed
          n = Number(n);
      
          // Return true/false
          return n === 0 || !!(n && !(n%2));
        }
        global.isEven = isEven;
      
        // Returns true if n is an integer and (n+1) is even
        // Returns false if n is not an integer or (n+1) is not even
        // Empty string evaluates to zero so returns false (zero is even)
        function isOdd(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Return true/false
          return n === 0 || !!(n && (n%2));
        }
        global.isOdd = isOdd;
      
      }(this));
      
      4 trả về
      // Returns true if:
      //
      //    n is an integer that is evenly divisible by 2
      //
      // Zero (+/-0) is even
      // Returns false if n is not an integer, not even or NaN
      // Guard against empty string
      
      (function (global) {
      
        function basicTests(n) {
      
          // Deal with empty string
          if (n === '') 
            return false;
      
          // Convert n to Number (may set to NaN)
          n = Number(n);
      
          // Deal with NaN
          if (isNaN(n)) 
            return false;
      
          // Deal with infinity - 
          if (n === Number.NEGATIVE_INFINITY || n === Number.POSITIVE_INFINITY)
            return false;
      
          // Return n as a number
          return n;
        }
      
        function isEven(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Convert to Number and proceed
          n = Number(n);
      
          // Return true/false
          return n === 0 || !!(n && !(n%2));
        }
        global.isEven = isEven;
      
        // Returns true if n is an integer and (n+1) is even
        // Returns false if n is not an integer or (n+1) is not even
        // Empty string evaluates to zero so returns false (zero is even)
        function isOdd(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Return true/false
          return n === 0 || !!(n && (n%2));
        }
        global.isOdd = isOdd;
      
      }(this));
      
      5. Thông thường bạn sẽ sử dụng toán tử Equality (===) để so sánh này, nhưng vì boolean được chuyển đổi thành một số trên toán tử
      // Returns true if:
      //
      //    n is an integer that is evenly divisible by 2
      //
      // Zero (+/-0) is even
      // Returns false if n is not an integer, not even or NaN
      // Guard against empty string
      
      (function (global) {
      
        function basicTests(n) {
      
          // Deal with empty string
          if (n === '') 
            return false;
      
          // Convert n to Number (may set to NaN)
          n = Number(n);
      
          // Deal with NaN
          if (isNaN(n)) 
            return false;
      
          // Deal with infinity - 
          if (n === Number.NEGATIVE_INFINITY || n === Number.POSITIVE_INFINITY)
            return false;
      
          // Return n as a number
          return n;
        }
      
        function isEven(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Convert to Number and proceed
          n = Number(n);
      
          // Return true/false
          return n === 0 || !!(n && !(n%2));
        }
        global.isEven = isEven;
      
        // Returns true if n is an integer and (n+1) is even
        // Returns false if n is not an integer or (n+1) is not even
        // Empty string evaluates to zero so returns false (zero is even)
        function isOdd(n) {
      
          // Do basic tests
          if (basicTests(n) === false)
            return false;
      
          // Return true/false
          return n === 0 || !!(n && (n%2));
        }
        global.isOdd = isOdd;
      
      }(this));
      
      8, bình đẳng đã giành được công việc ở đây vì hầu hết đã gõ hàm.

      Với tính chất không xác định của việc sử dụng cho chức năng này và được đặt tên, tôi nói rằng những điều sau đây sẽ an toàn khi sử dụng:

      function isEven(num) {
        return isNaN(num) && num !== false && num !== true ? false : num % 2 == 0;
      }
      

      hoặc cho những người thích mã có thể đọc được:

      function isEven(num) {
        if(num !== false && num !== true && !isNaN(num)) {
          return num % 2 == 0;
        } else return false;
      }
      

    • @Evan đánh nó ngay trên đầu

      Ngoài ra chưa đề cập đến == nhanh hơn ===, vì vậy nếu bạn biết bạn đang làm gì thì đó là nhanh hơn để sử dụng == khi có thể nhưng nếu bạn không chắc chắn, hãy sử dụng ===
      the == when applicable but if you are not sure, use ===

      Chúc mừng

  9. bao gồm

    bao gồm

    main () {int n; clrscr (); printf (nhập một số: \ n,); scanf (Hồi%dv, & n); if (n%2 == 0) {printf (số chẵn số); } other {printf (số lẻ số lẻ); } getch (); }
    {
    int n;
    clrscr();
    printf(“Enter a Number:\n”);
    scanf(“%d”,&n);
    if(n%2==0)
    {
    printf(“Even Number”);
    }
    else
    {
    printf(“Odd Number”);
    }
    getch();
    }

    • Này, tôi chỉ tự hỏi phương trình là gì (n % 2 == 0) thực sự đang làm?
      I was just wondering
      What is the equation “(n % 2 == 0)” actually doing?

    • Cú pháp lẻ là:

      Có chức năng chẵn…

      !isEven(number)

  10. Và điều gì sẽ xảy ra nếu giá trị của người Viking là một số cơ sở 10 số?

    function isEven(value) {
     value.toString(10)%2==0?true:false;
    }
    

  11. var a; var b = của Odd lẻ; var c = ngay cả a = parseInt (window.prompt (Hồi Nhập giá trị: Tiết)); Document.Write (Giá trị của bạn là:, chk (a));
    var b=”ODD”;
    var c=”EVEN”;
    a=parseInt(window.prompt(“Enter The Value:”));
    document.write(“Your Value is: “,chk(a));

    hàm chk (a) {if (a%2 == 0) return c; khác trở lại b; }
    {
    if (a%2==0)
    return c;
    else
    return b;
    }

  12. hàm isEven (value) {return! x%2; }
    return !x%2;
    }

    • Làm thế nào tôi có thể viết một hàm, có một số và trả về đúng hoặc sai nếu giá trị vừa hơn 0 và lẻ?

  13. Hoặc chúng ta có thể sử dụng bitwise và toán tử.

    hàm oddoreven (x) {return (x & 1)? "lẻ thậm chí"; }
    return ( x & 1 ) ? “odd” : “even”;
    }

  14. Sắp xếp các phần tử mảng, với số chẵn đầu tiên theo sau là số lẻ. (Document.Write và nhập giá trị bằng lời nhắc) (JavaScript) PLSSS giúp tôi ..

  15. Tôi sử dụng modulo rất nhiều trên giường, ví dụ, để ghi lại tiến trình:

    if (0 === đã xử lý % 1000) {Sprintf (bản ghi % s được xử lý, đã xử lý)}
    sprintf(“%s records processed”, processed)
    }

  16. nơi để nhập các mã này và nơi để xem rằng một con số là kỳ lạ .. làm ơn cho tôi biết

    • Chào @Mahnoor,

      Đoạn trích này sẽ đi vào bất kỳ tệp JavaScript nào mà bạn đang phục vụ trên trang web.

      Cheers!

  17. Hoặc bạn có thể làm một cái gì đó như thế này.

    const isEven = value => ( value % 2  === 0 ? true : false )
    
    

    • Chỉ cần theo dõi nhanh: Bạn cũng có thể cần một cái gì đó như Babel (https://babeljs.io/), nếu hệ thống của bạn không hỗ trợ ES6. Cảm tạ! Một

    • Bạn thậm chí có thể rút ngắn điều này nhiều hơn bằng cách làm một cái gì đó như:

      const isEven = value => value % 2 === 0
      

  18. // Thay đổi giá trị của

    function isEven(num) {
      return isNaN(num) && num !== false && num !== true ? false : num % 2 == 0;
    }
    
    3 để kiểm tra mã của bạn

    // Nếu số bằng 1, thì nên in vào bảng điều khiển. // Nếu số bằng 2, thì thậm chí nên được in vào bảng điều khiển.
    //If number equals 2, then Even should be printed to the console.

    số var = 2;

    if (số%2 === 1) {console.log (Hồi lẻ,)} other {console.log (ngay cả ngay cả }
    console.log(“odd”)
    } else{
    console.log(“Even”);
    }

  19. Bất cứ ai cũng có thể giải thích những gì 2 === 0 có nghĩa là hoặc cách nó hoạt động

    • Ba dấu hiệu bằng nhau === là một toán tử so sánh nghiêm ngặt của người Viking. Nó đánh giá xem các nội dung ở cả hai bên có giống nhau hay không. Nếu là họ, nó trả về đúng; Nếu không, thì sai. Trong trường hợp này, 2 không giống như 3, vì vậy nó sẽ trả về sai.

Để lại một câu trả lời

Làm thế nào để bạn kiểm tra xem một số chẵn hay số lẻ trong javascript?

Sử dụng bitwise hoặc toán tử khi một số trải qua bitwise hoặc với 1 và nếu kết quả là cùng một số, thì đó là một số lẻ. Nếu số được tăng thêm 1, thì đó là một số chẵn. Lấy 1011 hoặc 1 1011 0001 ------ 1011 Ở đây, các bit đầu ra bằng 11 do đó 11 là một số lẻ.When a number undergoes bitwise OR with 1 and if the result is the same number, it is an odd number. If the number gets incremented by 1, it is an even number. Take 1011 OR 1 1011 0001 ------ 1011 Here the output bits are equal to 11 hence 11 is an odd number.

Làm thế nào để bạn kiểm tra xem một số là chẵn?

Nếu một số được chia đều cho 2 không còn còn lại, thì đó là chẵn.Bạn có thể tính toán phần còn lại với toán tử modulo % như số này % 2 == 0.Nếu một số chia cho 2 để lại một phần còn lại là 1, thì số này là số lẻ.. You can calculate the remainder with the modulo operator % like this num % 2 == 0 . If a number divided by 2 leaves a remainder of 1, then the number is odd.

Làm thế nào để bạn kiểm tra xem một số chẵn hoặc sử dụng lẻ &?

Sau đó, liệu Num có hoàn toàn chia hết cho 2 hay không được kiểm tra bằng toán tử Modulus %.Nếu số hoàn toàn chia hết cho 2, số biểu thức kiểm tra%2 == 0 đánh giá thành 1 (true).Điều này có nghĩa là số là chẵn.Tuy nhiên, nếu biểu thức kiểm tra đánh giá là 0 (sai), số đó là số lẻ.

Làm thế nào để bạn kiểm tra xem một số là lẻ hay thậm chí trong jQuery?

Bộ chọn: lẻ chọn từng phần tử với số chỉ mục lẻ (như: 1, 3, 5, v.v.).Các số chỉ mục bắt đầu từ 0. Điều này chủ yếu được sử dụng cùng với một bộ chọn khác để chọn mọi phần tử được lập chỉ mục lẻ trong một nhóm (như trong ví dụ trên).Mẹo: Sử dụng: thậm chí bộ chọn để chọn các thành phần với số chỉ mục. (like: 1, 3, 5, etc.). The index numbers start at 0. This is mostly used together with another selector to select every odd indexed element in a group (like in the example above). Tip: Use the :even selector to select elements with even index numbers.