Explorer
JavaScript

Write a function `celsiusToFahrenheit(celsius)` that converts a temperature from Celsius to Fahrenheit using the formula F = C × 9/5 + 32.

Problem Statement

<p>Write a function <code>celsiusToFahrenheit(celsius)</code> that converts a temperature from Celsius to Fahrenheit using the formula F = C × 9/5 + 32.</p>

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

💡 Hint 1: The formula is straightforward: multiply by 9/5 and add 32. 💡 Hint 2: Be careful with operator precedence. 💡 Hint 3: return celsius * 9 / 5 + 32;

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

  1. Understand Problem Contract: Identify input arguments, return type expectations, and edge cases (empty inputs, nullish values).
  2. Choose Core Mechanism: Use modern JavaScript patterns (the formula is straightforward: multiply by 9/5 and add 32).
  3. Implement Logic: Handle state and transformations efficiently (be careful with operator precedence).
  4. 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

Time Complexity O(N) linear scan over input
Space Complexity O(1) constant auxiliary space (or O(N) output)

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.

Convert Celsius to Fahrenheit

Easy

Write a function celsiusToFahrenheit(celsius) that converts a temperature from Celsius to Fahrenheit using the formula F = C × 9/5 + 32.

Example Scenarios
1Example 1
Input: celsius = 0
Output: 32
Explanation:

0 * 9/5 + 32 = 32.

2Example 2
Input: celsius = 100
Output: 212
Explanation:

100 * 9/5 + 32 = 212.

Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
[32,212,-40]