ใครช่วยชี้ให้ฉันดูรหัสเพื่อตรวจสอบว่าตัวเลขใน JavaScript เป็นเลขคู่หรือคี่
%
ดำเนินการมากกว่า&
และ 2 ในขณะที่&
เร็วกว่าในทางทฤษฎีมันไม่สำคัญเลย
ใครช่วยชี้ให้ฉันดูรหัสเพื่อตรวจสอบว่าตัวเลขใน JavaScript เป็นเลขคู่หรือคี่
%
ดำเนินการมากกว่า&
และ 2 ในขณะที่&
เร็วกว่าในทางทฤษฎีมันไม่สำคัญเลย
คำตอบ:
ใช้รหัสด้านล่าง:
function isOdd(num) { return num % 2;}
console.log("1 is " + isOdd(1));
console.log("2 is " + isOdd(2));
console.log("3 is " + isOdd(3));
console.log("4 is " + isOdd(4));
1 หมายถึงเลขคี่ในขณะที่ 0 หมายถึงเลขคู่
0
หรือ1
(หรือNaN
ถ้าคุณป้อนสิ่งที่ไม่ใช่ตัวเลขและไม่สามารถรวมเข้าด้วยกันได้) ซึ่งจะใช้ได้ดีกับสถานการณ์ส่วนใหญ่ แต่ถ้าคุณต้องการตัวจริงtrue
หรือfalse
:return (num % 2) == 1;
num
ไม่ใช่จำนวนเต็ม ซึ่งจะยังคงใช้งานได้หากคุณเปรียบเทียบisOdd(1.5)==true
(เพราะค่าเศษส่วนไม่เท่ากับtrue
) แต่จะดีกว่าหากฟังก์ชันส่งคืนtrue
หรือfalse
ตามนัยโดยชื่อ "isOdd"
return !!(num % 2)
เพื่อรับบูลีน
ใช้ตัวAND
ดำเนินการระดับบิต
function oddOrEven(x) {
return ( x & 1 ) ? "odd" : "even";
}
function checkNumber(argNumber) {
document.getElementById("result").innerHTML = "Number " + argNumber + " is " + oddOrEven(argNumber);
}
checkNumber(17);
<div id="result" style="font-size:150%;text-shadow: 1px 1px 2px #CE5937;" ></div>
หากคุณไม่ต้องการค่าส่งคืนสตริง แต่เป็นค่าบูลีนให้ใช้สิ่งนี้:
var isOdd = function(x) { return x & 1; };
var isEven = function(x) { return !( x & 1 ); };
X % Y
!
function isEven(n){return !(n & 1);}
.
x & 1
ตรวจสอบว่าบิตสุดท้ายถูกตั้งค่าเป็นตัวเลขหรือไม่ (เพราะ 1 เป็นตัวเลขที่มีบิตทั้งหมดตั้งค่าเป็น 1 ยกเว้นบิตนัยสำคัญน้อยที่สุด ): ถ้าเป็นเช่นนั้นตัวเลขจะเป็นเลขคี่
คุณสามารถทำสิ่งนี้:
function isEven(value){
if (value%2 == 0)
return true;
else
return false;
}
if (condition) { answer=true; } else { answer=false; }
answer = (bool) condition;
ลดฟังก์ชั่นของคุณfunction isEven(value) { return (bool) (value%2 == 0); }
และเราทุกคนจะมีความสุข
(bool)
(ซึ่งจะทำให้เกิดข้อผิดพลาด) และในกรณีใด ๆ ที่คุณไม่จำเป็นต้อง: return value%2 == 0;
จะทำงานเนื่องจาก==
ผู้ประกอบการส่งกลับบูลีน
answer = !!(condition)
ควรจะเป็นสิ่งที่ชอบ ประเด็นที่ฉันพยายามทำก็คือคุณสามารถreturn value%2==0
ทำได้และไม่จำเป็นต้องกังวลกับเงื่อนไข
value%2===0
function isEven(x) { return (x%2)==0; }
function isOdd(x) { return !isEven(x); }
ฉันต้องทำให้อาร์เรย์มีขนาดใหญ่จริง ๆ ที่มีจำนวนคู่มาก
ไม่ใช้โมดูลัส (%) มันให้ส่วนที่เหลือของตัวเลขสองจำนวนที่คุณหาร
Ex. 2 % 2 = 0 because 2/2 = 1 with 0 remainder.
Ex2. 3 % 2 = 1 because 3/2 = 1 with 1 remainder.
Ex3. -7 % 2 = -1 because -7/2 = -3 with -1 remainder.
ซึ่งหมายความว่าถ้าคุณแก้ไขหมายเลข x 2 คุณจะได้รับ 0 หรือ 1 หรือ -1 0 จะหมายความว่ามันเป็นเลขคู่ สิ่งอื่นใดก็หมายความว่ามันแปลก
สิ่งนี้สามารถแก้ไขได้ด้วยโค้ดขนาดเล็ก:
function isEven(value) {
if (value%2 == 0)
return true;
else
return false;
}
หวังว่าจะช่วย :)
เช่นเดียวกับหลายภาษา Javascript มีตัวดำเนินการโมดูลัส%
ซึ่งค้นหาส่วนที่เหลือ หากไม่มีเหลือหลังจากการหารด้วย 2 ตัวเลขจะเท่ากับ:
// this expression is true if "number" is even, false otherwise
(number % 2 == 0)
นี่เป็นสำนวนที่ใช้กันทั่วไปมากสำหรับการทดสอบเลขจำนวนเต็มคู่
ด้วย bitwise codegolfing:
var isEven=n=>(n&1)?"odd":"even";
ฟังก์ชั่นง่าย ๆ ที่คุณสามารถผ่านได้ ใช้โอเปอเรเตอร์แบบโมดูโล%
:
var is_even = function(x) {
return !(x % 2);
}
is_even(3)
false
is_even(6)
true
return !(x % 2);
ใช้ส่วนขยายของฉัน:
Number.prototype.isEven=function(){
return this % 2===0;
};
Number.prototype.isOdd=function(){
return !this.isEven();
}
แล้วก็
var a=5;
a.isEven();
== เท็จ
a.isOdd();
== ทรู
หากคุณไม่แน่ใจว่าเป็นตัวเลขหรือไม่ให้ทดสอบโดยการแยกสาขาต่อไปนี้
if(a.isOdd){
a.isOdd();
}
อัปเดต:
หากคุณจะไม่ใช้ตัวแปร:
(5).isOdd()
ปรากฎว่ากระบวนทัศน์กระบวนทัศน์ดีกว่ากระบวนทัศน์ OOP โดยวิธีการที่ฉันทำ โปรไฟล์ใน FIDDLEนี้ อย่างไรก็ตามวิธี OOP ยังสวยที่สุด
คุณสามารถใช้คำสั่ง for และเงื่อนไขเพื่อพิจารณาว่าตัวเลขหรือชุดของตัวเลขเป็นเลขคี่หรือไม่:
for (var i=1; i<=5; i++)
if (i%2 !== 0) {
console.log(i)
}
สิ่งนี้จะพิมพ์ทุกเลขคี่ระหว่าง 1 ถึง 5
เพิ่งดำเนินการหนึ่งใน Adobe Dreamweaver .. มันทำงานได้อย่างสมบูรณ์ ฉันใช้ถ้า (isNaN (mynmb))
เพื่อตรวจสอบว่าค่าที่กำหนดเป็นตัวเลขหรือไม่และฉันยังใช้ Math.abs (mynmb% 2)เพื่อแปลงจำนวนลบให้เป็นค่าบวกและคำนวณ
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body bgcolor = "#FFFFCC">
<h3 align ="center"> ODD OR EVEN </h3><table cellspacing = "2" cellpadding = "5" bgcolor="palegreen">
<form name = formtwo>
<td align = "center">
<center><BR />Enter a number:
<input type=text id="enter" name=enter maxlength="10" />
<input type=button name = b3 value = "Click Here" onClick = compute() />
<b>is<b>
<input type=text id="outtxt" name=output size="5" value="" disabled /> </b></b></center><b><b>
<BR /><BR />
</b></b></td></form>
</table>
<script type='text/javascript'>
function compute()
{
var enter = document.getElementById("enter");
var outtxt = document.getElementById("outtxt");
var mynmb = enter.value;
if (isNaN(mynmb))
{
outtxt.value = "error !!!";
alert( 'please enter a valid number');
enter.focus();
return;
}
else
{
if ( mynmb%2 == 0 ) { outtxt.value = "Even"; }
if ( Math.abs(mynmb%2) == 1 ) { outtxt.value = "Odd"; }
}
}
</script>
</body>
</html>
<script>
function even_odd(){
var num = document.getElementById('number').value;
if ( num % 2){
document.getElementById('result').innerHTML = "Entered Number is Odd";
}
else{
document.getElementById('result').innerHTML = "Entered Number is Even";
}
}
</script>
</head>
<body>
<center>
<div id="error"></div>
<center>
<h2> Find Given Number is Even or Odd </h2>
<p>Enter a value</p>
<input type="text" id="number" />
<button onclick="even_odd();">Check</button><br />
<div id="result"><b></b></div>
</center>
</center>
</body>
if (X % 2 === 0){
} else {
}
แทนที่ X ด้วยหมายเลขของคุณ (อาจมาจากตัวแปร) คำสั่ง If ทำงานเมื่อตัวเลขเป็นเลขคู่ Else เมื่อเป็นเลขคี่
ถ้าคุณแค่อยากรู้ว่าตัวเลขที่ให้มาเป็นเลขคี่หรือไม่:
if (X % 2 !== 0){
}
อีกครั้งแทนที่ X ด้วยตัวเลขหรือตัวแปร
ทุกเลขคี่เมื่อหารด้วยสองใบที่เหลือเป็น 1 และทุกเลขคู่เมื่อหารด้วยศูนย์จะทำให้ใบเหลือเป็นศูนย์ ดังนั้นเราสามารถใช้รหัสนี้
function checker(number) {
return number%2==0?even:odd;
}
แล้ว ...
var num = 3 //instead get your value here
var aa = ["Even", "Odd"];
alert(aa[num % 2]);
นี่คือสิ่งที่ฉันทำ
//Array of numbers
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,32,23,643,67,5876,6345,34,3453];
//Array of even numbers
var evenNumbers = [];
//Array of odd numbers
var oddNumbers = [];
function classifyNumbers(arr){
//go through the numbers one by one
for(var i=0; i<=arr.length-1; i++){
if (arr[i] % 2 == 0 ){
//Push the number to the evenNumbers array
evenNumbers.push(arr[i]);
} else {
//Push the number to the oddNumbers array
oddNumbers.push(arr[i]);
}
}
}
classifyNumbers(numbers);
console.log('Even numbers: ' + evenNumbers);
console.log('Odd numbers: ' + oddNumbers);
ด้วยเหตุผลบางอย่างฉันต้องแน่ใจว่าความยาวของอาเรย์นั้นน้อยลง เมื่อฉันไม่ทำเช่นนั้นฉันจะได้รับ "undefined" ในองค์ประกอบสุดท้ายของอาร์เรย์คี่ตัวเลข
เมื่อคุณต้องการทดสอบว่าตัวแปรบางตัวเป็นค่าคี่แรกคุณควรทดสอบว่าเป็นจำนวนเต็มหรือไม่ นอกจากนี้โปรดสังเกตว่าเมื่อคุณคำนวณส่วนที่เหลือเป็นจำนวนลบผลลัพธ์จะเป็นค่าลบ ( -3 % 2 === -1
)
function isOdd(value) {
return typeof value === "number" && // value should be a number
isFinite(value) && // value should be finite
Math.floor(value) === value && // value should be integer
value % 2 !== 0; // value should not be even
}
หาก Number.isInteger พร้อมใช้งานคุณอาจลดความซับซ้อนของรหัสนี้เป็น:
function isOdd(value) {
return Number.isInteger(value) // value should be integer
value % 2 !== 0; // value should not be even
}
หมายเหตุ: ที่นี่เราทดสอบvalue % 2 !== 0
แทนที่จะเป็นเพราะvalue % 2 === 1
-3 % 2 === -1
หากคุณไม่ต้องการ-1
ผ่านการทดสอบนี้คุณอาจต้องเปลี่ยนบรรทัดนี้
นี่คือกรณีทดสอบบางส่วน:
isOdd(); // false
isOdd("string"); // false
isOdd(Infinity); // false
isOdd(NaN); // false
isOdd(0); // false
isOdd(1.1); // false
isOdd("1"); // false
isOdd(1); // true
isOdd(-1); // true
การใช้%
จะช่วยให้คุณทำสิ่งนี้ ...
คุณสามารถสร้างฟังก์ชั่นสองอย่างเพื่อทำเพื่อคุณ ... ฉันชอบฟังก์ชั่น separte ที่ไม่ได้แนบกับ Number ใน Javascript เช่นนี้ซึ่งตรวจสอบว่าคุณผ่านหมายเลขหรือไม่:
ฟังก์ชั่นแปลก:
var isOdd = function(num) {
return 'number'!==typeof num ? 'NaN' : !!(num % 2);
};
ฟังก์ชั่นแม้กระทั่ง:
var isEven = function(num) {
return isOdd(num)==='NaN' ? isOdd(num) : !isOdd(num);
};
และเรียกมันว่าสิ่งนี้:
isOdd(5); // true
isOdd(6); // false
isOdd(12); // false
isOdd(18); // false
isEven(18); // true
isEven('18'); // 'NaN'
isEven('17'); // 'NaN'
isOdd(null); // 'NaN'
isEven('100'); // true
หนึ่งซับใน ES6 เพียงเพราะมันสะอาด
const isEven = (num) => num % 2 == 0;
isOdd("str")
ควรเป็นเท็จ isOdd(1.223)
และisOdd(-1.223)
ควรเป็นเท็จ isOdd(0)
ควรเป็นเท็จ isOdd(-1)
ควรเป็นจริง function isOdd(n) {
// Must be a number
if (isNaN(n)) {
return false;
}
// Number must not be a float
if ((n % 1) !== 0) {
return false;
}
// Integer must not be equal to zero
if (n === 0) {
return false;
}
// Integer must be odd
if ((n % 2) !== 0) {
return true;
}
return false;
}
JS Fiddle (หากจำเป็น): https://jsfiddle.net/9dzdv593/8/
โซลูชัน Javascript 1 ซับ สำหรับผู้ที่ไม่สนใจการอ่าน
const isOdd = n => !(isNaN(n) && ((n % 1) !== 0) && (n === 0)) && ((n % 2) !== 0) ? true : false;
(n % 2) !== 0
(n % 2) === 0
(2) คำแนะนำของฉันคือการหลีกเลี่ยง!!(n % 2)
เพราะ (a) มันมีประสิทธิภาพช้ากว่า(n % 2) !== 0
( jsperf.com/notnot-vs-strict-not ) (b) มันแฮ็ค - มันรวมค่าความเท็จ0
เข้าไว้false
และ (c) มันคลุมเครือ (ภาษาการเขียนโปรแกรมระดับสูงไม่ควรอ่านเหมือนกับ Pascal เพราะประสิทธิภาพ - นั่นเป็นหน้าที่ของคอมไพเลอร์) (3) ใช่{}
คำสั่งบล็อกที่หายไปจะส่งผลให้เกิดปัญหาหลายประการ (ตามที่ได้รับการปรับปรุงในคำตอบของฉัน)
if (0) call1(), assign = 0, call2()
แต่คำสั่งเดียวไม่เลว: if (0) return; if (0) ;; if (0); break; if (0) continue;
และยังไงก็ตามฉันชอบใช้คำสั่ง break-line block ต่อไปเมื่อฉัน เงื่อนไขระยะยาวแบบอินไลน์
isNaN(n)
โง่ - แน่ใจว่าคุณครอบคลุมNaN
กรณี แต่isOdd(null)
, isOdd(undefined)
, isOdd({x:1})
ผลตอบแทนทั้งหมดfalse
ซึ่งผมคิดว่าจะเป็นข้อผิดพลาด; นอกเสียจากว่าคุณจะระบุเพียงว่าฟังก์ชั่นของคุณมีพฤติกรรมที่ถูกต้องในโดเมนที่กำหนด: เฉพาะอินพุตประเภทตัวเลข ในกรณีนี้ให้ทำisNaN
เครื่องหมายในช่องนั้นและบังคับให้ผู้ใช้โทรหามันด้วยประเภทที่ถูกต้อง การเขียนโปรแกรมการป้องกันเป็นอย่างยิ่งใหญ่ จากนั้นฟังก์ชั่นของคุณจะถูกทำให้ง่ายขึ้นisOdd = x => Math.floor(x) === x && x & 1 === 1
- ส่งคืนค่าที่ชัดเจนtrue
หรือfalse
ไม่จำเป็นต้องมีค่า
null
, undefined
และวัตถุ{}
ไม่ได้เลขคี่และดังนั้นจึงกลับมาทำงานfalse
- ไม่แน่ใจว่าทำไมคุณพิจารณาว่ามีข้อผิดพลาด การisNaN
ตรวจสอบสำหรับประสิทธิภาพ (ไม่ใช่เพื่อการป้องกัน) มันช่วยให้ฟังก์ชั่นออกก่อนกำหนดโดยไม่ต้องทำการตรวจสอบอื่น ๆ
ฉันจะใช้สิ่งนี้เพื่อส่งคืนบูลีน:
function isOdd (n) {
return !!(n % 2);
// or ((n % 2) !== 0).
}
มันจะทำงานกับทั้งหมายเลขที่ไม่ได้ลงนามและลงนาม เมื่อกลับมาโมดูลัส-1
หรือว่ามันจะได้รับการแปลเป็น1
true
วิธีการแก้ปัญหาที่ไม่ใช่โมดูลัส:
var is_finite = isFinite;
var is_nan = isNaN;
function isOdd (discriminant) {
if (is_nan(discriminant) && !is_finite(discriminant)) {
return false;
}
// Unsigned numbers
if (discriminant >= 0) {
while (discriminant >= 1) discriminant -= 2;
// Signed numbers
} else {
if (discriminant === -1) return true;
while (discriminant <= -1) discriminant += 2;
}
return !!discriminant;
}
วิธีการทำงานที่มากขึ้นในจาวาสคริปต์ที่ทันสมัย:
const NUMBERS = "nul one two three four five six seven ocho nueve".split(" ")
const negate = f=> (...args)=> !f(...args)
const isOdd = n=> NUMBERS[n % 10].indexOf("e")!=-1
const isEven = negate(isOdd)
ใน ES6:
const isOdd = num => num % 2 == 1;
ด้วยการใช้ผู้ประกอบการที่สามคุณจะพบตัวเลขคู่คี่:
var num = 2;
result = (num % 2 == 0) ? 'even' : 'odd'
console.log(result);
ใช้งานได้กับอาร์เรย์:
function evenOrOdd(numbers) {
const evenNumbers = [];
const oddNumbers = [];
numbers.forEach(number => {
if (number % 2 === 0) {
evenNumbers.push(number);
} else {
oddNumbers.push(number);
}
});
console.log("Even: " + evenNumbers + "\nOdd: " + oddNumbers);
}
evenOrOdd([1, 4, 9, 21, 41, 92]);
สิ่งนี้ควรออกจากระบบ: 4,92 1,9,21,41
เพียงตัวเลข:
function evenOrOdd(number) {
if (number % 2 === 0) {
return "even";
}
return "odd";
}
console.log(evenOrOdd(4));
สิ่งนี้ควรจะได้ผลลัพธ์แม้แต่คอนโซล