πŸ“

Divide Numbers

Divide Numbers
μž…λ¬Έ 기초 문법 +5pts

Problem

Write a function that divides two numbers. Return 0 if dividing by zero.

Examples

Input: divide(10, 2)
Output: 5
Input: divide(10, 0)
Output: 0

Explanation

이 λ¬Έμ œλŠ” **λ‚˜λˆ—μ…ˆ μ—°μ‚°μž**와 **μ˜ˆμ™Έ 처리**λ₯Ό ν•™μŠ΅ν•©λ‹ˆλ‹€. 0으둜 λ‚˜λˆ„λŠ” νŠΉμˆ˜ν•œ 경우λ₯Ό μ²˜λ¦¬ν•˜λŠ” 방법을 λ°°μ›λ‹ˆλ‹€. **λ‚˜λˆ—μ…ˆ μ—°μ‚°μž (/)** JavaScriptμ—μ„œ `/`λŠ” 두 숫자의 λ‚˜λˆ—μ…ˆμ„ μˆ˜ν–‰ν•©λ‹ˆλ‹€. μ •μˆ˜λΌλ¦¬ λ‚˜λˆ λ„ μ†Œμˆ˜μ  κ²°κ³Όκ°€ λ‚˜μ˜¬ 수 μžˆμŠ΅λ‹ˆλ‹€: - 10 / 2 = 5 - 7 / 2 = 3.5 **0으둜 λ‚˜λˆ„κΈ°** JavaScriptμ—μ„œ 0으둜 λ‚˜λˆ„λ©΄ μ—λŸ¬κ°€ μ•„λ‹Œ Infinityκ°€ λ©λ‹ˆλ‹€: - 10 / 0 = Infinity - -10 / 0 = -Infinity ν•˜μ§€λ§Œ 이 λ¬Έμ œμ—μ„œλŠ” 0을 λ°˜ν™˜ν•˜λ„λ‘ μš”κ΅¬ν•©λ‹ˆ...

View detailed explanation β†’

Key Concepts

λ‚˜λˆ—μ…ˆ μ—°μ‚°μž (/) 0으둜 λ‚˜λˆ„κΈ° 처리 if 쑰건문 early return νŒ¨ν„΄
Time: O(1) Space: O(1)
solution.js
Ctrl + Enter
Run tests to see results here.