Coding Challenge Cheat Sheet

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

1. String to Array

str.split() // Split the string based on the type passed as an argument

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

arr.join() // join the array based on argument passed

arr.toString() // converts array to string as it is

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

Last updated