Index
Overview


The max is the maximum element in a list of numbers.

As always, the solutions shown are not the only solutions (there may be are many).

Also see suggested approach....

Manual Solutions


This is often what we will do first when using a recipe approach

These old pencil-and-paper style manual solutions are often our best friends.

We simply iterate through the list looking for the largest (max) number.
given [10, 40, 30]

let max = first elem = 10

iterate through elements
We'll start at second position (first would also work)
40 > 10
	yes
	let max = 40

30 > 40
	no

final max = 40


Unit Tests


We'll take a test first approach.

Example Tests
Just this small group of tests covers a lot of the scenarios for 'mode'.
testMax() {
	const unit = new Stats([40, 10, 30, 20]);
	this.assertEquals(40, unit.max());
}

testMinCountOne() {
	const unit = new Stats([40]);
	this.assertEquals(40, unit.max());
}

testMinEmptyCase() {
	const unit = new Stats([]);
	this.assertEquals(null, unit.max());
}



Recipe


Special Cases
Eliminate special cases right away. That simplifies the rest of the recipe.
let count = this.count()
if (count == 0) return null
Solving
We were able to basically just follow our manual solution here.
nums = this.nums
max = nums[0]
i = 1
while (i < nums.length)
	next = nums[i]
	if (next > max)
		max = next
	i++
return max