Underscore.js _.isOdd() 方法
_.isOdd ()方法检查给定值是否为奇数,相应地返回一个布尔值。
句法:
_.isOdd( value );
参数:
- value:要检查奇数的给定值。
返回值:此方法返回一个布尔值(如果给定值是奇数则返回真,否则返回假)。
注意:这在普通 JavaScript 中不起作用,因为它需要安装 underscore.js contrib 库。 Underscore.js contrib 库可以使用npm install underscore-contrib 安装
示例 1:
Javascript
// Defining underscore contrib variable
var _ = require('underscore-contrib');
// Checking
console.log("The Value is Odd : " + _.isOdd(41));
Javascript
// Defining underscore contrib variable
var _ = require('underscore-contrib');
// Checking
console.log("The Value is Odd : " + _.isOdd(42));
Javascript
// Defining underscore contrib variable
var _ = require('underscore-contrib');
// Checking
console.log("The Value is Odd : " + _.isOdd(0));
Javascript
// Defining underscore contrib variable
var _ = require('underscore-contrib');
// Checking
console.log("The Value is Odd : " + _.isOdd("GFG"));
输出:
The Value is Odd : true
示例 2:
Javascript
// Defining underscore contrib variable
var _ = require('underscore-contrib');
// Checking
console.log("The Value is Odd : " + _.isOdd(42));
输出:
The Value is Odd : false
示例 3:对于值:0,此方法返回 false。
Javascript
// Defining underscore contrib variable
var _ = require('underscore-contrib');
// Checking
console.log("The Value is Odd : " + _.isOdd(0));
输出:
The Value is Odd : false
示例 4:对于非整数值,此方法返回 false。
Javascript
// Defining underscore contrib variable
var _ = require('underscore-contrib');
// Checking
console.log("The Value is Odd : " + _.isOdd("GFG"));
输出:
The Value is Odd : false