const findLargestDifference = (array) => {
// Check if the array is empty or has only one element
if (array.length < 2) {
throw new Error('Array must have at least two elements to find a difference.');
}
// Initialize variables to store the minimum and maximum values
let [min, max] = [array[0], array[0]];
// Iterate through the array to find the minimum and maximum values
for (const num of array) {
if (num > max) {
max = num;
}
if (num < min) {
min = num;
}
}
// Return the difference between the maximum and minimum values
return max - min;
};
let age = 50;
function printAge() {
console.log(age);
let age = 30;
}
printAge();
// Example usage
const numberArray = [10, 8, 6, 3, 9, 12, 3];
const largestDifference = findLargestDifference(numberArray);
console.log(largestDifference); // Output: 9 (12 - 3)
To embed this project on your website, copy the following code and paste it into your website's HTML: