Ngay cả những con số là những con số chính xác chia hết cho 2. numbers are those numbers that are exactly divisible by 2.
Toán tử còn lại
// program to check if the number is even or odd
// take input from the user
const number = prompt["Enter a number: "];
//check if the number is even
if[number % 2 == 0] {
console.log["The number is even."];
}
// if the number is odd
else {
console.log["The number is odd."];
}
0 cung cấp phần còn lại khi được sử dụng với một số. Ví dụ,const number = 6;
const result = number % 4; // 2
Do đó, khi
// program to check if the number is even or odd
// take input from the user
const number = prompt["Enter a number: "];
//check if the number is even
if[number % 2 == 0] {
console.log["The number is even."];
}
// if the number is odd
else {
console.log["The number is odd."];
}
0 được sử dụng với 2, số đó là ngay cả khi phần còn lại bằng không. Nếu không, số là lẻ.2, the number is even if the remainder is zero. Otherwise, the number is odd.Ví dụ 1: Sử dụng nếu ... khác
// program to check if the number is even or odd
// take input from the user
const number = prompt["Enter a number: "];
//check if the number is even
if[number % 2 == 0] {
console.log["The number is even."];
}
// if the number is odd
else {
console.log["The number is odd."];
}
Đầu ra
Enter a number: 27 The number is odd.
Trong chương trình trên,
// program to check if the number is even or odd
// take input from the user
const number = prompt["Enter a number: "];
//check if the number is even
if[number % 2 == 0] {
console.log["The number is even."];
}
// if the number is odd
else {
console.log["The number is odd."];
}
2 kiểm tra xem số có được không. Nếu phần còn lại là 0, số đồng đều.even. If the remainder is 0, the number is even.Trong trường hợp này, 27 % 2 bằng 1. Do đó, số là số lẻ.27 % 2 equals to 1. Hence, the number is odd.
Chương trình trên cũng có thể được viết bằng toán tử ternary.
Ví dụ 2: Sử dụng toán tử ternary
// program to check if the number is even or odd
// take input from the user
const number = prompt["Enter a number: "];
// ternary operator
const result = [number % 2 == 0] ? "even" : "odd";
// display the result
console.log[`The number is ${result}.`];
Đầu ra
Enter a number: 5 The number is odd.
Trong bài viết này, bạn sẽ học và kiểm tra xem một số là số chẵn hay số lẻ có và không sử dụng đầu vào người dùng trong JavaScript. Dưới đây là danh sách chương trình JavaScript để kiểm tra số chẵn hoặc số lẻ:
- Kiểm tra chẵn hoặc lẻ mà không có đầu vào của người dùng
- Nhận số từ người dùng, kiểm tra và in xem số chẵn hay số lẻ. Đầu ra trực tiếp của chương trình này cũng được cung cấp
Kiểm tra số chẵn hoặc số lẻ trong javascript
Chương trình này kiểm tra xem giá trị được lưu trữ trong biến số là số chẵn hay số lẻ. Phương thức tài liệu.write [] ghi dữ liệu vào đầu ra HTML.num variable is an even or an odd number. The document.write[] method writes the data to an HTML output.
doctype html> var num=6; if[num%2==0] document.write[num + " is an Even Number"]; else document.write[num + " is an Odd Number"];
Lưu mã này trong một tệp với tiện ích mở rộng .html. Bây giờ hãy mở tệp trong trình duyệt web. Đây là đầu ra:.html extension. Now open the file in a web browser. Here is the output:
Nếu bạn thay đổi giá trị của Num bằng 7 và lưu mã trong cùng một tệp. Sau đó, đây là đầu ra sau khi làm mới hoặc mở lại tệp trong trình duyệt web:num with 7 and save the code in same file. Then here is the output after refreshing or re-opening the file in a web browser:
Cho phép người dùng nhập số
Đây là chương trình thực tế để kiểm tra xem một số được nhập bởi người dùng có phải là số chẵn hay số lẻ trong JavaScript hay không. Chương trình này nhận được đầu vào từ người dùng sử dụng TextBox.
doctype html> var num, temp; function fun[] { num = parseInt[document.getElementById["num"].value]; if[num] { temp = document.getElementById["resPara"]; temp.style.display = "block"; if[num%2==0] document.getElementById["res"].innerHTML = "Even"; else document.getElementById["res"].innerHTML = "Odd"; } } Enter the Number: Check It is an Number
Đây là đầu ra mẫu của nó:
Bây giờ nhập bất kỳ số nào nói 17 và nhấp vào nút Kiểm tra để in một thông báo cho biết số đã nhập là số chẵn hay số lẻ như trong ảnh chụp nhanh của đầu ra cuối cùng được đưa ra dưới đây:17 and click on Check button to print a message that tells whether the entered number is an even or an odd number as shown in the snapshot of final output given below:
Mã sau:
là mã CSS, ẩn một phần tử HTML nơi nó được viết. Bởi vì nó được viết bên trong thẻ P [đoạn], có ID là Respara. Vì vậy, đoạn này được ẩn ban đầu.P [paragraph] tag, whose id is resPara. So this paragraph gets hidden initially.
Khi người dùng nhấp vào nút Kiểm tra, một hàm có tên Fun [] sẽ được gọi. Tất cả các câu lệnh của chức năng này được thực thi. Tuyên bố JavaScript sau:Check button, a function named fun[] gets called. All the statements of this function gets executed. The following JavaScript statement:
num = parseInt[document.getElementById["num"].value];
nói rằng, giá trị int [số nguyên] của phần tử HTML có id được khởi tạo thành biến số. Và tuyên bố được đưa ra dưới đây:int [integer] value of an HTML element whose id is num gets initialized to num variable. And the statement given below:
temp.style.display = "block";
nói rằng, một phần tử HTML có ID được lưu trữ trong TEMP, có thể nhìn thấy sau khi thực hiện câu lệnh JavaScript này. Và câu lệnh JavaScript sau:temp, gets visible after executing this JavaScript statement. And the following JavaScript statement:
document.getElementById["res"].innerHTML = "Even";
nói rằng, văn bản thậm chí còn được ghi vào một phần tử HTML có ID là res.Even gets written to an HTML element whose id is res.
Đầu ra trực tiếp của chương trình trước
Dưới đây là đầu ra trực tiếp được sản xuất bởi chương trình JavaScript trước đây khi kiểm tra chẵn hoặc số lẻ:
JavaScript Kiểm tra trực tuyến
«JavaScript TutorialCodesCracker» CodesCracker »