Day 02: 30 Days of Codewars.js
Tags: challenge, javascript, todayilearned, 100daysofcode
Published: 2020-04-23 21:49:40

> ## Table Of Contents
   * [Sort array by string length](#kata-1)
     - 📃 [Description](#kata-1-desc)
     - 🤔 [Thinking](#kata-1-think)
     - 👨‍💻 [Code](#kata-1-code)
   * [Descending Order](#kata-2)
     - 📃 [Description](#kata-2-desc)
     - 🤔 [Thinking](#kata-2-think)
     - 👨‍💻 [Code](#kata-2-code)
   * [Playing with digits](#kata-3)
     - 📃 [Description](#kata-3-desc)
     - 🤔 [Thinking](#kata-3-think)
     - 👨‍💻 [Code](#kata-3-code)
     - 🐞 [Bugs](#kata-3-bugs)
     - 🏁 [Finally](#kata-3-finally)

---

<a name="kata-1"></a>
# Sort array by string length: ✍ by [Steve hopkinson](https://www.codewars.com/users/stevehopkinson)

<a name="kata-1-desc"></a>
### 📃 Description 
> Write a function that takes an array of strings as an argument and returns a sorted array according to the string length `Ascendingly`

<a name="kata-1-think"></a>
### 🤔 Thinking 
I didn't have to think that much.
I know how `sort` works it takes a compare function or two arguments the first is the first element and the second is the second element and it keeps increment the index like the `map` or the `filter` methods

<a name="kata-1-code"></a>
### 👨‍💻 Code
```javascript
const sortByLength = arr => arr.sort((x, y) => x.length - y.length);
```

---

<a name="kata-2"></a>
# Descending Order : ✍ by [TastyOs](https://www.codewars.com/users/TastyOs)

<a name="kata-2-desc"></a>
### 📃 Description
> make a function that can take any non-negative integer as an argument and return it with its digits in descending order. Essentially, rearrange the digits to create the highest possible number.

<a name="kata-2-think"></a>
### 🤔 Thinking
It's pretty much like the last one 
I will make the number an array then sort it then make it a number again It's easy 

<a name="kata-2-code"></a>
### 👨‍💻 Code
```javascript
const descendingOrder = num => {
  const digitArr = Array.from(String(num), Number);
  const sortedArr = digitArr.sort((x, y) => y - x);
  const sorted = Number(sortedArr.join(""));
  
  return sorted;
}
```
---

<a name="kata-3"></a>
# Playing with digits : ✍ by [g964](https://www.codewars.com/users/g964)

<a name="kata-3-desc"></a>
### 📃 Description
> Given a positive integer `n` written as abcd... (a, b, c, d... being digits) and a positive integer p
> - we want to find a positive integer k, if it exists, such as the sum of the digits of n taken to the successive powers of p is equal to k * n.
> #### In other words:
Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k
If it is the case we will return k, if not return -1.

<a name="kata-3-think"></a>
### 🤔 Thinking
I will make the first number `n` an array, like in the last kata.
Then I will map over it and `power` it with `p + index` index is the second argument in the map method.
Then `Add` it all.
Then check if it's equal to `n * k`

<a name="kata-3-code"></a>
### 👨‍💻 Code

```javascript
const digPow = (n, p) => {
  const k = n * p;
  const numArr = Array.from(String(n), Number);
  const sum = numArr.reduce((acc, elm, i) => (elm ** (p + i)) + acc);  
  const result = sum === k ? p : -1;

  return result
}
```

<a name="kata-3-bugs"></a>
### 🐞 Bugs
*I don't understand what he want from this description*

<a name="kata-3-finally"></a>
### 🏁 Finally
After many times trying and trying and reading the discussion section I gave up and googled it but I said to myself if I keep doing that I will never be better and I won't be better, So I didn't look at the solutions and reminded myself that I don't have to cheat so when I write this post it will be bugless 

> ### *If I didn't have any bugs, I didn't learn anything new*

So I unlocked the solutions and looked at them and then I submit this

```javascript
const digPow = (n, p) => {
  const numArr = Array.from(String(n), Number);
  const sum = numArr.reduce((acc, elm, i) => acc + (elm ** (p + i)), 0);  
  const result = sum === (n * p) ? p : -1;

  return sum % n ? -1 : sum / n;
}
```

---

> ## Sorry for the similar katas, But I will take it as today was my `num to arr` / `sort` day