How to find the largest odd number in an array in javascript

青灯夜游
Release: 2022-10-19 18:09:36
Original
1533 people have browsed it

Implementation steps: 1. Use filter() to filter the array and return all odd elements. The syntax is "function f(v){if(v%2!=0){return true;}else{return false; }}var b=arr.filter(f);" will obtain an array containing all odd elements; 2. Use Math.max() and the expansion operator "..." to obtain the maximum value in the odd array, syntax "Math.max(...b)".

How to find the largest odd number in an array in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In javascript, you can use the filter() and Math.max() methods to find the largest odd number in an array.

Implementation steps:

Step 1: Use the filter() method to get all odd numbers

The filter() method can filter array elements and return elements that meet specified conditions. Just use

and if you want to get all the odd numbers, you just need to filter all the even numbers in the array and only return the technology.

Example: Return all odd numbers

var a = [2,3,4,5,6,7,8];
function f (value) {
	if (value % 2 != 0) {
		return true;
	}else{
		 return false;
	}
}
var b = a.filter(f);
console.log(b);
Copy after login

Output result:

How to find the largest odd number in an array in javascript

As you can see, a value containing Array of all odd elements

Step 2: Use Math.max() method and spread operator...Get the maximum value in the odd array

Math.max() method can accept a series of numbers and return the largest number.

Note: The Math.max() method cannot directly accept an array as a parameter; if you want to use the Math.max() method to return the maximum value of the array, you need to use the expansion operator ...use together.

Expand operator... can expand the array elements and take them out one by one and pass them to the Math.max() method as parameters.

Example:

var a = [2,3,4,5,6,7,8];
function f (value) {
	if (value % 2 != 0) {
		return true;
	}else{
		 return false;
	}
}
var b = a.filter(f);
console.log(b);
var max = Math.max(...b);
console.log("数组的最大奇数为:"+max);
Copy after login

How to find the largest odd number in an array in javascript

Function description:

1, filter() method

The filter() method can return elements in the array that meet specified conditions. Just use

array.filter(function callbackfn(Value,index,array),thisValue)
Copy after login

function callbackfn(Value,index,array): a callback function, which cannot be omitted, and can accept up to three parameters:

  • value: The value of the current array element, cannot be omitted.

  • index: The numeric index of the current array element.

  • array: The array object to which the current element belongs.

The return value is a new array containing all the values ​​for which the callback function returns true. If the callback function returns false for all elements of array , the length of the new array is 0.

2. Math.max() method

The Math.max() method returns the maximum number as an input parameter. If there are no parameters, it returns -Infinity.

Math.max(value1, value2, ... valueN ) ;
Copy after login

The following are the details of the parameters:

  • value1, value2, ... valueN : Number.

【 Related recommendations: javascript video tutorial, programming video

The above is the detailed content of How to find the largest odd number in an array in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template