π
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:
5Input:
divide(10, 0)Output:
0Explanation
μ΄ λ¬Έμ λ **λλμ μ°μ°μ**μ **μμΈ μ²λ¦¬**λ₯Ό νμ΅ν©λλ€. 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.