user
Build a JavaScript function that finds the largest and smallest number in an unsorted integer array. PLAINFORMAT
assistant
// Function to find the largest and smallest number in an array
function findLargestAndSmallest(arr) {
    // Initialize variables for largest and smallest numbers
    let largest = arr[0];
    let smallest = arr[0];

    // Loop through the array
    for (let i = 1; i < arr.length; i++) {
        // If current element is larger than the largest, update largest
        if (arr[i] > largest) {
            largest = arr[i];
        }
        // If current element is smaller than the smallest, update smallest
        else if (arr[i] < smallest) {
            smallest = arr[i];
        }
    }

    // Return an object with the largest and smallest numbers
    return {largest: largest, smallest: smallest};
}