## Table Of Contents

- Sort array by string length

- 📃 Description
- 🤔 Thinking
- 👨💻 Code
- Descending Order

- 📃 Description
- 🤔 Thinking
- 👨💻 Code
- Playing with digits

- 📃 Description
- 🤔 Thinking
- 👨💻 Code
- 🐞 Bugs
- 🏁 Finally

# Sort array by string length: ✍ by Steve hopkinson

### 📃 Description

Write a function that takes an array of strings as an argument and returns a sorted array according to the string length

`Ascendingly`

### 🤔 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

### 👨💻 Code

```
const sortByLength = arr => arr.sort((x, y) => x.length - y.length);
```

# Descending Order : ✍ by TastyOs

### 📃 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.

### 🤔 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

### 👨💻 Code

```
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;
}
```

# Playing with digits : ✍ by g964

### 📃 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.

### 🤔 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`

### 👨💻 Code

```
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
}
```

### 🐞 Bugs

*I don't understand what he want from this description*

### 🏁 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

```
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

## Top comments (0)