Coding Challenge Cheat Sheet
While coding, it's better to have some tricks and methods handy
1. String to Array
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
// 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
Was this helpful?