Znajdź, czy zmienna jest podzielna przez 2
Jak obliczyć, czy zmienna jest podzielna przez 2? Ponadto muszę wykonać funkcję, jeśli jest, i wykonać inną funkcję, jeśli nie jest.
10 answers
Użyj modułu:
// Will evaluate to true if the variable is divisible by 2
variable % 2 === 0
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-05-12 17:01:07
Poważnie, nie ma wtyczki jQuery do sprawdzania nieparzystych/parzystych?
Cóż, już nie-wypuszczenie" piekarnika " wtyczki jQuery na licencji MIT, aby sprawdzić, czy dana liczba jest nieparzysta/parzysta.
Kod źródłowy jest również dostępny pod adresem http://jsfiddle.net/7HQNG/
Test-suity są dostępne w http://jsfiddle.net/zeuRV/
(function() {
/*
* isEven(n)
* @args number n
* @return boolean returns whether the given number is even
*/
jQuery.isEven = function(number) {
return number % 2 == 0;
};
/* isOdd(n)
* @args number n
* @return boolean returns whether the given number is odd
*/
jQuery.isOdd = function(number) {
return !jQuery.isEven(number);
};
})();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-03 12:56:49
Nie potrzebujesz jQuery. Wystarczy użyć operatora Modulo JavaScript.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-05-12 17:01:15
Możesz użyć operatora modułu w ten sposób, bez potrzeby jQuery. Wystarczy zastąpić alerts
swoim kodem.
var x = 2;
if (x % 2 == 0)
{
alert('even');
}
else
{
alert('odd')
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-05-12 17:03:09
Możesz to zrobić w lepszy sposób (do 50% szybciej niż operator modulo):
Odd: x & 1 nawet:!(x & 1)
Reference: High Performance JavaScript, 8. - >Operatory Bitowe
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-27 14:15:39
Możesz także:
if (x & 1)
itsOdd();
else
itsEven();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-05-12 18:43:34
var x = 2;
x % 2 ? oddFunction() : evenFunction();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-05-12 17:49:32
Proszę napisać następujący kod w konsoli:
var isEven = function(deep) {
if (deep % 2 === 0) {
return true;
}
else {
return false;
}
};
isEven(44);
Uwaga: zwróci true, jeśli wprowadzony numer jest nawet fałszywy.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-01 08:08:56
Użyj modułu, ale.. Powyższa zaakceptowana odpowiedź jest nieco niedokładna. Uważam, że ponieważ x jest typem liczby w JavaScript, operator powinien być przypisaniem podwójnym zamiast potrójnym, jak TAK:
x % 2 == 0
Pamiętaj, aby zadeklarować również swoje zmienne, Więc oczywiście ta linia nie może być zapisana samodzielnie. :- ) Zwykle używane jako if
oświadczenie. Mam nadzieję, że to pomoże.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-14 19:57:37
Array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Array.each {|x / puts x if X % 2 = = 0 }
Ruby: D
2 4 6 8 10
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-17 21:07:40