Finding the Floor Number in an Array
This challenge focuses on extracting the floor number from an array of objects, each representing a building with a height. Determining the floor number is a common task in architectural and engineering applications, and this problem tests your ability to iterate through data structures and perform calculations based on specific criteria. Your goal is to write a JavaScript function that efficiently identifies the floor number for each building based on its height.
Problem Description
You are given an array of objects, where each object represents a building and has a height property (a number). The floor number of a building is determined by rounding the building's height down to the nearest whole number. Your task is to create a JavaScript function called calculateFloorNumbers that takes this array as input and returns a new array containing the floor number for each corresponding building. The original array should remain unchanged.
Key Requirements:
- The function must accept an array of building objects as input.
- Each building object must have a
heightproperty, which is a number. - The function must return a new array containing the floor number (rounded down) for each building.
- The function should handle cases where the height is not a number gracefully (e.g., return 0 or
NaNfor invalid heights). - The original input array should not be modified.
Expected Behavior:
The function should iterate through the input array, calculate the floor number for each building using Math.floor(), and store the result in a new array. The new array should be returned.
Edge Cases to Consider:
- Empty input array: Should return an empty array.
- Buildings with zero height: Should return 0 as the floor number.
- Buildings with negative height: Should return 0 as the floor number (or handle as you see fit, but document your choice).
- Buildings with non-numeric height values (e.g., strings, null, undefined): Should return
NaNor 0 (document your choice).
Examples
Example 1:
Input: [
{ height: 10.5 },
{ height: 5 },
{ height: 2.2 }
]
Output: [10, 5, 2]
Explanation: 10.5 rounded down is 10, 5 rounded down is 5, and 2.2 rounded down is 2.
Example 2:
Input: [
{ height: 0 },
{ height: -3.1 },
{ height: 7.8 }
]
Output: [0, 0, 7]
Explanation: 0 rounded down is 0, -3.1 rounded down is -4 (but we're returning 0), and 7.8 rounded down is 7.
Example 3:
Input: [
{ height: "abc" },
{ height: 12 },
{ height: null }
]
Output: [NaN, 12, NaN]
Explanation: "abc" and null are not numbers, so the floor number is NaN. 12 rounded down is 12.
Constraints
- The input array will contain objects.
- Each object will have a
heightproperty. - The
heightproperty can be a number, string, null, undefined, or any other data type. - The function must complete within a reasonable time (e.g., less than 1 second for an array of 1000 buildings).
- The function should be written in JavaScript.
Notes
Consider using Array.map() for a concise and efficient solution. Think about how to handle invalid height values (non-numeric) to ensure your function doesn't crash and provides meaningful results. Document your approach to handling edge cases clearly in your code. The use of Math.floor() is essential for correctly calculating the floor number.