HomeAbout

Array Iteration

Iteration

First two of ascending index iteration

Handle base cases (0 and 1) separately when iterating through large number of times for a pattern:

if(array.length === 0) return "array is empty" if(array.length === 1) { // when array length is one let onlyElement = array[0]; return onlyElement; } if(array.length % 2 === 0) { // when array length is even } if(array.length % 2 === 1) { // when array length is odd }

Control Flow

Always exit from an if condition

When you have multiple if conditions, make sure to return to prevent further evaluation.

Make Use of Boolean

const condition1: boolean = (i) => i % 3 === 0; const condition2: boolean = (j) => j.toString().includes('someStrValue'); const someBooleanCheck: boolean = (x) => { return condition1 || condition2 } for(let i=0; i<n; i++) { if(someBooleanCheck(i)) { // this behavior } else { // different behavior } }

Lookahead

Be aware that when performing a lookahead, i+1 beyond the last element of array will be undefined.

Termination length:

if( someTerminalCondition || i+1 == array.length) { return "Current element (i) is the last element of the array. Next i does not exist and is beyond the array definition."}
AboutContact