Write a function `celsiusToFahrenheit(celsius)` that converts a temperature from Celsius to Fahrenheit using the formula F = C × 9/5 + 32.
Problem Statement
Examples
Input: celsius = 0
Output: 32
Explanation: 0 * 9/5 + 32 = 32.
Input: celsius = 100
Output: 212
Explanation: 100 * 9/5 + 32 = 212.
Complexity
Time Complexity: O(N)
Space Complexity: O(1)
Hints
Editorial & Approach
Problem Overview & Intuition
To solve Convert Celsius to Fahrenheit, we consider the execution characteristics of JavaScript engines. Write a function `celsiusToFahrenheit(celsius)` that converts a temperature from Celsius to Fahrenheit using the formula F = C × 9/5 + 32. By utilizing idiomatic language constructs and clean algorithmic principles, we can accomplish this with optimal time and memory usage.
Step-by-Step Approach
- Understand Problem Contract: Identify input arguments, return type expectations, and edge cases (empty inputs, nullish values).
- Choose Core Mechanism: Use modern JavaScript patterns (the formula is straightforward: multiply by 9/5 and add 32).
- Implement Logic: Handle state and transformations efficiently (be careful with operator precedence).
- Return Result: Ensure proper return format and preserve caller context if applicable.
Optimal Implementation (JavaScript)
function celsiusToFahrenheit(celsius) {
return celsius * 9 / 5 + 32;
}
Complexity Analysis
Edge Cases & Corner Traps Handled
- Empty or boundary inputs (empty arrays, strings, zero length).
- Type checks and unexpected values (e.g.
null,undefined, negative numbers). - Closure preservation and memory isolation between separate invocations.