Day 04: 30 Days of Codewars.js
Tags: javascript, 100daysofcode, todayilearned, challenge
Published: 2020-04-25 21:41:46

> ## Table Of Contents
   * [Multiples of 3 or 5](#kata-1)
     - 📃 [Description](#kata-1-desc)
     - 🤔 [Thinking](#kata-1-think)
     - 👨‍💻 [Code](#kata-1-code)
     - 🐞 [Bugs](#kata-1-bugs)
   * [Validate Credit Card Number](#kata-2)
     - 📃 [Description](#kata-2-desc)
     - 🤔 [Thinking](#kata-2-think)
     - 👨‍💻 [Code](#kata-2-code)
     - 🐞 [Bugs](#kata-2-bugs)
     - 🏁 [Finally](#kata-2-finally)

---

<a name="kata-1"></a>
# Multiples of 3 or 5 : ✍ by [jhoffner](https://www.codewars.com/users/jhoffner)

<a name="kata-1-desc"></a>
### 📃 Description
> If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

<a name="kata-1-think"></a>
### 🤔 Thinking
I'm gonna make a `for` loop will check if a number less than the limit divisible `%` by 3 or `||` 5
Then push it to an array
Then add it

<a name="kata-1-code"></a>
### 👨‍💻 Code
```javascript
const solution = num => {
  let divBy3Or5 = [];
  
  for(let i = 1; i < num; i++) {
    ((i % 3 === 0) || (i % 5 === 0)) ? divBy3Or5.push(i) : null;  
  }
  
  return divBy3Or5.reduce((acc, elm) => acc + elm, 0);
}
```

<a name="kata-1-bugs"></a>
### 🐞 Bugs
I think it's a stupid code like it has to be a better way to solve this kata 
**Please if you have a better solution let me know in the comments**

---

<a name="kata-2"></a>
# Validate Credit Card Number : ✍ by [mcclaskc](https://www.codewars.com/users/mcclaskc)

<a name="kata-2-desc"></a>
### 📃 Description
> In this Kata, you will implement the [Luhn Algorithm](http://en.wikipedia.org/wiki/Luhn_algorithm), which is used to help validate credit card numbers.
Given a positive integer of up to 16 digits, return `true` if it is a valid credit card number, and `false` if it is not.

##### Here is the algorithm:
Double every other digit, scanning from right to left, starting from the second digit (from the right).
Another way to think about it is: if there are an even number of digits, double every other digit starting with the first; if there are an odd number of digits, double every other digit starting with the second:
```javascript
1714 ==> [1*, 7, 1*, 4] ==> [2, 7, 2, 4]

12345 ==> [1, 2*, 3, 4*, 5] ==> [1, 4, 3, 8, 5]

891 ==> [8, 9*, 1] ==> [8, 18, 1]
```
If a resulting number is greater than `9`, replace it with the sum of its own digits (which is the same as subtracting `9` from it):
```javascript
[8, 18*, 1] ==> [8, (1+8), 1] ==> [8, 9, 1]

// OR

[8, 18*, 1] ==> [8, (18-9), 1] ==> [8, 9, 1]
```
Sum all of the final digits:
```javascript
[8, 9, 1] ==> 8 + 9 + 1 = 18
```
Finally, take that sum and divide it by `10`. If the remainder equals zero, the original credit card number is valid.
```javascript
18 (modulus) 10 ==> 8 , which is not equal to 0, so this is not a valid credit card number
```

<a name="kata-2-think"></a>
### 🤔 Thinking
I will create an array out of the credit card number
Then check its length if it's even I will loop over it starting in the first index `0` jumping one index at a time like `0, 2, 4, 6, n.length` if its odd I will do the same but starting in the second element index number `1`
Then double it and add them into another array and then `sum` it 
Then divide it by 10 and check if its remainder is `equal` to `0`

<a name="kata-2-code"></a>
### 👨‍💻 Code
```javascript
const validate = num => {
  let numArr = Array.from(String(num), Number);
  
  if (numArr.length % 2 === 0) {
    for(let i = 0; i< numArr.length; i+=2) {
      numArr[i] *= 2;
    }
  } else {
    for(let i = 1; i< numArr.length; i+=2) {
      numArr[i] *= 2;
    }
  }
  
  const lessThan18Arr = numArr.map(num => num > 9 ? num - 9 : num)
  
  const sum = lessThan18Arr.reduce((acc, elm) => acc + elm, 0)
  
  return sum % 10 === 0;
}
```

<a name="kata-2-bugs"></a>
### 🐞 Bugs
I think it's the Time complexity `(Both Solutions takes about 1000ms give or take 100ms)`
And there is repetitive code 
Not DRY (Don't Repeat Yourself)

<a name="kata-2-finally"></a>
### 🏁 Finally
```javascript
const validate = num => {
  let numArr = Array.from(String(num), Number);  
  let i = numArr.length % 2 === 0 ? 0 : 1; 

  while(i < numArr.length) {
    numArr[i] *= 2;
    i+=2;
  }
  
  const lessThan18Arr = numArr.map(num => num > 9 ? num - 9 : num)
  
  const sum = lessThan18Arr.reduce((acc, elm) => acc + elm, 0)
  
  return sum % 10 === 0;
}
```

##### <center>If you know a better way to solve any of the previous katas let me know in the comment</center>

---

*<center>Thanks for reading, I really appreciate it.</center>*