find the maximum difference
between two numbers in an array
[ nested for loops | Math.abs ]

Find the maximum difference between two numbers in an array

Write a function that takes an array of numbers and returns the maximum difference between any two of its numbers.


Example ...

Enter an array ...

[4, 2, 6, 5, 3] array

The function returns 4 because 6 - 2 = 4 returns the maximum difference between any two of its numbers.

Arrays are used to store multiple values in a single variable.

Each value is called an element, and each element has a numeric position in the array, known as its index.

Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.

Arrays can contain any data type, including numbers, strings, and objects.

const arr1 = [2, 4, 6]; array

arr1[0]; element at index 0 → 2

arr1[1]; element at index 1 → 4

arr1[2]; element at index 2 → 6

arr1[3]; element at index 3 → undefined index not found


Numbers are used to represent both integer and floating-point values.

Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴

let num1 = 5; → number

let num2 = 2.5; → number

let num3 = num1 + num2;

console.log(num3); returns ↴

7.5 → number


Find the maximum difference between two numbers in an array using ↴

for loop → executes a block of code a number of times.

Math.abs() static method returns the absolute value of a number.


for loop repeatedly executes a block of code until a specified condition evaluates to false.

The loop runs a block of code a set number of times, defined by an initialization, a condition, and an increment.

for (let x = 0; x < 4; x++) {

console.log(x);

}

Loop variable x is initialized to 0

Condition x < 4 is checked before each iteration.

The loop will continue to run as long as x is less than 4

The loop repeatedly executes a block of code 4 times, from 0 to 3

For each iteration of the loop, the current value of x is printed to the console.

After each iteration, x is incremented by 1 x++

When x reaches 4 the condition evaluates to false, terminating the loop.

0

1

2

3 → printed to console


Nested for loops

A nested loop is a loop inside another loop.

A nested for loop consists of an outer for loop and one or more inner for loops

Each time the outer for loop repeats, the control re-enters inside the inner for loop and starts a new execution.

The outer loop iterates through the rows, while the inner loop iterates through the columns.

const rows = [1, 2, 3];

const columns = ["A", "B", "C"];

for (let x = 0; x < rows.length; x++) { → outer loop

for (let y = 0; y < columns.length; y++) { → inner loop

console.log(`row ${rows[x]} : column ${columns[y]}`);

}

}

returns ↴

row 1 : column A

row 1 : column B

row 1 : column C

row 2 : column A

row 2 : column B

row 2 : column C

row 3 : column A

row 3 : column B

row 3 : column C → printed to console

Generate a multiplication table.

for (let x = 1; x <= 12; x++) { → outer loop

let row = "";

for (let y = 1; y <= 10; y++) { → inner loop

row += (x * y) + "\t";

}

console.log(row);

}

returns ↴

1  2  3  4  5  6  7  8  9  10

2  4  6  8  10 12 14 16 18 20

3  6  9  12 15 18 21 24 27 30

4  8  12 16 20 24 28 32 36 40

5  10 15 20 25 30 35 40 45 50

6  12 18 24 30 36 42 48 54 60

7  14 21 28 35 42 49 56 63 70

8  16 24 32 40 48 56 64 72 80

9  18 27 36 45 54 63 72 81 90

10 20 30 40 50 60 70 80 90 100

11 22 33 44 55 66 77 88 99 110

12 24 36 48 60 72 84 96 108 120 → printed to console

rows ⇣ outer loop

columns ⇢ inner loop

The outer loop iterates through the rows ⇣ 1 to 12

The inner loop iterates through the columns ⇢ 1 to 10.


Math.abs static method returns the absolute value of a number.

The method takes one argument, which is the number for which you want to find the absolute value, and it returns a positive number or zero.

Math.abs(7) 7

Math.abs(-7) 7

Math.abs(12.345) 12.345

Math.abs(-12.345) 12.345

The result is always a positive number or 0


Initialize a variable to hold the array find the maximum difference between two numbers.

const array1 = [4, 8, 6, 2, 5, 7, 9, 3]; → user input


Define a function maxDifference to find the maximum difference between two numbers.

function maxDifference(arr) {}

The function takes an array as input arr and returns the maximum number between two numbers found in the array.

Initialize a variable to store the maximum difference found.

let maxDiff = 0 maxDiff

Outer loop to iterate through each element in the array.

for (let x = 0; x < arr.length; x++) {

Inner loop to compare the current element with the following elements.

for (let y = x + 1; y < arr.length; y++) {

Calculate the absolute difference between the two elements.

const diff = Math.abs(arr[y] - arr[x]) diff

Check if the calculated difference is greater than the current maxDiff.

if (diff > maxDiff) {

If true, update maxDiff.

maxDiff = diff

Return the maximum difference found.

return maxDiff


Call the function with ↴

maxDifference(array1);


Find the maximum difference between two numbers in an array.

const array1 = [4, 8, 6, 2, 5, 7, 9, 3];

function maxDifference(arr) {

let maxDiff = 0;

for (let x = 0; x < arr.length; x++) {

for (let y = x + 1; y < arr.length; y++) {

const diff = Math.abs(arr[y] - arr[x]);

if (diff > maxDiff) {

maxDiff = diff;

}

}

}

return maxDiff;

}

call function

maxDifference(array1); returns ↴

7

9 - 2 = 7

Find the maximum difference between two numbers in an array