0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

最大値を求める(Iterable を与えて、JavaScript)

Last updated at Posted at 2022-10-18

こんにちは。
Iterable を与えて、その中の最大値を求めました(JavaScript)。最小値も同様かと思います。

console.log(max(range(3)));  //  ==>  2

function max(iterable) {
	let max = -Infinity;
	for (const item of iterable) {
		if (item > max) {max = item}
	}
	return max;
}

function* range(n) {
	let i = 0;
	while (i < n) {yield i++;}
}
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?