Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
Function Signature
function addDigits(num);
Example 1:
Input:
num = 38
Output:2
Explanation: The process is 3+8 = 11, 1+1 = 2. Since 2 has only one digit, 2 is returned.
Example 2:
Input:
num = 999
Output:9
Explanation: The process is 9+9+9 = 27, 2+7 = 9. Since 9 has only one digit, 9 is returned.
Solve this problem
Start your 7-day free trial to solve this problem in our code editor with instant feedback.
Start 7-Day Free Trial to Solve This ProblemRelated Problems
Two Sum
easyWrite a function `twoSum` that takes two integers `a` and `b` and returns their sum. ## Function Signature ```function twoSum(a, b);``` ### Example 1: > **Input:** `a = 3, b = 5`\ > **Output:** `8`\ > **Explanation:** The sum of `3` and `5` is `8`. ### Example 2: > **Input:** `a = -2, b = -4`\ > **Output:** `-6`\ > **Explanation:** The sum of `-2` and `-4` is `-6`. ### Example 3: > **Input:** `a = 100, b = 200`\ > **Output:** `300`\ > **Explanation:** The sum of `100` and `200` is `300`.
MathMultiply
easyWrite a function `multiply` that takes two integers `a` and `b` and returns their product. ## Function Signature ```function multiply(a, b);``` ### Example 1: > **Input:** `a = 3, b = 5`\ > **Output:** `15`\ > **Explanation:** The product of `3` and `5` is `15`. ### Example 2: > **Input:** `a = -2, b = -4`\ > **Output:** `8`\ > **Explanation:** The product of `-2` and `-4` is `8`. ### Example 3: > **Input:** `a = 0, b = 200`\ > **Output:** `0`\ > **Explanation:** The product of `0` and `200` is `0`.
MathIs Even
easyWrite a function `isEven` that takes an integer and returns true if it is even, false otherwise. ## Function Signature ```function isEven(num);``` ### Example 1: > **Input:** `num = 4`\ > **Output:** `true`\ > **Explanation:** The number 4 is even. ### Example 2: > **Input:** `num = 7`\ > **Output:** `false`\ > **Explanation:** The number 7 is not even. ### Example 3: > **Input:** `num = 0`\ > **Output:** `true`\ > **Explanation:** The number 0 is even.
Math