Day 1 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#728.Self-Dividing Numbers(Easy/JavaScript)
Tags: leetcode, javascript, algorithms, beginners
Published: 2022-02-09 15:39:30

**Intro:** I am a former accountant turned software engineer graduated from coding bootcamp in January 2022. Algorithms and Data Structure is an unavoidable part of interviews for most of the tech companies now. And one of my friends told me that you need to solve a medium leetcode problem under 60 seconds in order to get into the top tech companies.So I thought I'd start learning how to do it while job searching. 

Since I have no clue on how to solve any of the problems (even the easy ones), I thought there is no point for me to waste hours and can't get it figured out. **Here is my approach:**
- Pick a leetcode problem randomly or Online Assessment from targeted companies.
- Study 2 solutions from Youtube or LeetCode discussion section. One brute force solution, another one more optimal.
- Write a blog post with detailed explanation and do a verbal walk through to help understand the solutions better. 
- Code out the solution in LeetCode without looking at the solutions
- **Combat the forgetting curve:** Re-do the question for the next three days. And come back regularly to revisit the problem.

## Problem#728.Self-Dividing Numbers
`Difficulty: Easy` `Language: JavaScript`
<!-- `Company: JP Morgan Chase Apprenticeship 2022` -->


A **self-dividing number** is a number that is divisible by every digit it contains.

- For example, 128 is a **self-dividing number** because `128 % 1 == 0`, `128 % 2 == 0`, and `128 % 8 == 0`.

A **self-dividing number** is not allowed to contain the digit zero.

Given two integers `left` and `right`, return a list of all the **self-dividing numbers** in the range `[left, right]`.

**Example 1:**
```
Input: left = 1, right = 22
Output: [1,2,3,4,5,6,7,8,9,11,12,15,22]
```
**Example 2:**
```
Input: left = 47, right = 85
Output: [48,55,66,77]
```
**Constraints:**
`1 <= left <= right <= 104`

## Solution 1 with explaination:
```
var selfDividingNumbers = function(left, right) {

    let result = []

/*Declare an empty array with square brackets*/

    for (i = left; i <= right ; i++) {

/*Loop through each element in the array (see note 1)*/

        isSelfDividing(i)? result.push(i) : result; 

/*Use helper function (note 3) and conditional operator(note 2)

to push (note 4) desired result into the array*/

    }
    return result;
};

var isSelfDividing = function(n) {

/*This is the helper function (note 3)*/

    let number = n.toString();

/*convert n to a string (note 5)*/

    for (let digit of number) {

/*Iterate over the string (note 6)*/

        if(number % digit !==0 || digit === 0) return false;

/*If the remainder (note 7) of number mod by digit is not zero,
that means digit is not divisible by the number. Hence, the number
is not self-dividing. And division by zero is undefined*/

    }

    return true;

};
```
**Solution 1 Submission detail as of 2/9/2022** 
(Data below could vary since there are new submissions daily)
- Runtime: 98 ms, faster than 46.03% of JavaScript online submissions for Self Dividing Numbers.
- Memory Usage: 44.7 MB, less than 21.16% of JavaScript online submissions for Self Dividing Numbers.

## Solution 2 with explaination:
If solution 1 is brute forth, solution 2 runs faster and takes up a little less memory since string is not involved. **The key to this solution is to obtain the last digit of a number with "number % 10." ** You will realized that if you divide a number by 10 the remainder is the last digit of this number. For example, if you use 128 divided by 10 you will get 8, which is the last digit of 128. (it actually took me a while to note this).

```
var selfDividingNumbers = function(left, right) {

    let result = [];

/*create output array same as in solution 1*/

    for (i = left; i <= right; i++) {

/*Loop through each element in the array (see note 1)*/

        if (isSelfDivisible(i)) result.push(i)

/*Use helper function (note 3) to push (note 4) desired result
into the array*/

    }
    return result;
}

var isSelfDivisible = function(number) {

    let temp = number

/*create a temp variable*/

    while (temp > 0){

        let lastDigit = temp % 10

/*Use this line to obtain last digit of the number*/

        if(number % lastDigit !== 0) return null

/*Check if last digist is divisible by the number*/

        temp = Math.floor(temp / 10)

/*Now we test if second last digit is divisible
Note that this line will pass 128 without 0 to the next 
iteration. For example: Math.floor(128 / 10) = 12 (note 8).
Now that temp is 12. We can use temp % 10 again to get
second last digit 2 for another divisible testing.
*/

    }
    return number;
}
```
**Solution 2 Submission detail as of 2/9/2022** 
(Data below could vary since there are new submissions daily)
- Runtime: 68 ms, faster than 98.41% of JavaScript online submissions for Self Dividing Numbers.
- Memory Usage: 42.6 MB, less than 35.45% of JavaScript online submissions for Self Dividing Numbers.

********************************************************

**References:**

- [Note 1: For loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for)
- [Note 2: Conditional(tenary)operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)
- [Note 3: Helper Function](https://stackoverflow.com/questions/42837402/role-of-helper-functions/42837486#:~:text=A%20%22helper%20function%22%20is%20a,average%20value%20from%20that%20list.)
- [Note 4: Array Push Method] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push)
- [Note 5: toString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString)
- [Note 6: for...of loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of)
- [Note 7: Remainder Operator % (pronounced as mod)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder)
- [Note 8: Math.floor] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor)
- [LeetCode link for the problem](https://leetcode.com/problems/self-dividing-numbers/)
- [Youtube:Coding David](https://youtu.be/1A1cd0UMgC4)
- [LeetCode Discussion: nicobarbarese](https://leetcode.com/problems/self-dividing-numbers/discuss/1096550/simple-javascript-solution-with-a-helper-function-80-faster-runtime)
- [LeetCode Discussion: RinatRezyapov](https://leetcode.com/problems/self-dividing-numbers/discuss/1742741/Not-a-one-liner-but-easy-to-understand)
- [Blog Image Credit](https://www.istockphoto.com/portfolio/monsitj?mediatype=photography)