Find the Minimum Value in an Array
This challenge will test your ability to iterate through an array and identify the smallest numerical value it contains. Finding the minimum value is a fundamental operation in computer science, used in various algorithms like sorting, data analysis, and optimization problems.
Problem Description
Your task is to write a Javascript function that takes an array of numbers as input and returns the smallest number present in that array.
Requirements:
- The function should accept a single argument: an array of numbers.
- The function should return a single number: the minimum value found in the array.
- The function should handle arrays containing positive, negative, and zero values.
Expected Behavior:
- Given an array of numbers, the function should correctly identify and return the absolute smallest number.
- If the input array is empty, you should return
undefinedas there is no minimum value to find.
Edge Cases to Consider:
- An empty array.
- An array with only one element.
- An array containing duplicate minimum values.
- An array containing only negative numbers.
- An array containing a mix of positive, negative, and zero.
Examples
Example 1:
Input: [3, 1, 4, 1, 5, 9, 2, 6]
Output: 1
Explanation: The smallest number in the array is 1.
Example 2:
Input: [-5, -10, 0, 5, 10]
Output: -10
Explanation: The smallest number in the array is -10.
Example 3:
Input: []
Output: undefined
Explanation: The input array is empty, so there is no minimum value.
Example 4:
Input: [7]
Output: 7
Explanation: The array contains only one element, which is therefore the minimum.
Constraints
- The input will always be an array.
- The elements within the array will be numbers (integers or floating-point).
- The array can contain up to 10,000 elements.
Notes
Consider how you would initialize a variable to keep track of the minimum value. What would be a safe initial value? Think about iterating through the array and comparing each element to your current minimum.