> For the complete documentation index, see [llms.txt](https://sandeepamaranath.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sandeepamaranath.gitbook.io/notes/interview/technical/coding-challenge-cheat-sheet.md).

# Coding Challenge Cheat Sheet

While coding, it's better to have some tricks and methods handy

#### 1. String to Array

{% hint style="info" %}
str.split()    // Split the string based on the type passed as an argument
{% endhint %}

```javascript
const singleStr = "sandeep";

singleStr.split(); // ->    ["sandeep"]

singleStr.split(" "); // -> ["sandeep"]

singleStr.split(""); // ->  ["s","a","n","d","e","e","p"]


const spaceStr = "john doe";

spaceStr.split(); // ->    ["john doe"]         

spaceStr.split(" "); // -> ["john", "doe"]

spaceStr.split(""); // ->  ["j","o","h","n","","d","o","e"]


const commaStr = "jo,hn";

spaceStr.split(); // ->    ["jo,hn"]         

spaceStr.split(" "); // -> ["jo,hn"]

spaceStr.split(""); // ->  ["j","o",",","h","n"]

spaceStr.split(","); // ->  ["jo", "hn"]
```

#### 2. Array to String

{% hint style="info" %}
arr.join()    // join the array based on argument passed
{% endhint %}

{% hint style="info" %}
arr.toString() // converts array to string as it is&#x20;
{% endhint %}

```javascript
// join is to say - "Pick each element from array and join on the param passed"

const elements = ['Fire', 'Air', 'Water'];

elements.join()     // Fire,Air,Water -> not joining on anything but converted to string 

elements.join('')   // FireAirWater 

elements.join(' ')  // Fire Air Water 

elements.join(',')  // Fire,Air,Water 

elements.join('-')  // Fire-Air-Water 


elements.toString() // Fire,Air,Water  
```
