Javascript
Random String
btoa(Math.random()).substr(0, 10);
Generates a alphanumeric characters string with the specified length.
Heads or Tails?
Math.random() >= 0.5?console.log('heads'):console.log('tails');
Divides a random number between 0 and 1, checks if it is lesser or greater than 0.5(thus giving 50-50 probability), and returns heads or tails.
Reverse a string
str.split('').reverse().join('');
Where str is the string to reverse.
Rename an object key
delete Object.assign(objName, {['newKey']: objName['oldKey'] })['oldKey'];
Replaces an object's key with a new key
Get the siblings of a DOM element
export function getAllSiblingsOfElement(){
const siblings = ele => [].slice.call(ele.parentNode.children).filter((child) => (child !== ele));
return siblings;
}
Gets all the siblings of a DOM element.
Insert element after an element
export function insertElementAfter(){
const insertAfter = (ele, anotherEle) => anotherEle.parentNode.insertBefore(ele, anotherEle.nextSibling);
//OR
const insertAfter = (ele, anotherEle) => anotherEle.insertAdjacentElement('afterend', ele);
}
Inserts an element after the provided element.
Insert element before an element
export function insertElementBefore(){
const insertBefore = (ele, anotherEle) => anotherEle.parentNode.insertBefore(ele, anotherEle);
// OR
const insertBefore = (ele, anotherEle) => anotherEle.insertAdjacentElement('beforebegin', ele);
}
Inserts an element before the provided element.
Insert HTML after an element
export function insertHtmlAfter(){
const insertHtmlAfter = (html, ele) => ele.insertAdjacentHTML('afterend', html);
}
Inserts an HTML after the provided element.
Insert HTML before an element
export function insertHtmlBefore(){
const insertHtmlBefore = (html, ele) => ele.insertAdjacentHTML('beforebegin', html);
}
Inserts an HTML before the provided element.
Replace an element
export function replaceElement(){
const replace = (ele, newEle) => ele.parentNode.replaceChild(newEle, ele);
}
Replaces an element with another element.
Strip HTML from text
export function stripHtmlFromText(){
const stripHtml = html => (new DOMParser().parseFromString(html, 'text/html')).body.textContent || '';
}
Strips HTML from a given text.
Get unique letters
export function getUniqueLetters(chars){
return Array.from(new Set(chars.toLowerCase().replaceAll(' ','').split('')));
}
Strips duplicate charactes and spaces, then returns a collection of unique elements.
Get factorial
export function getFactorial(f){
let j=1;for(let i=1;i<=f;i++){j=i*j};return j};
}
Returns factorial of the given number.
Parse URLs inside string
export function replaceURLs(text) {
return text.replace( /(https?://[^s]+)/g, '<a href="$1" target="_blank">$1</a>' ); }
parses all URLs into <a> tags.
Calculate Factorial
function factorialCalculate(number) {
if (number === 0) {
return 1;
} else {
return number * factorialCalculate(number - 1);
}
}
Calculates the factorial of a number using a recursive function.
Simple fetch to call API
fetch(apiUrl)
.then(response => {
if (response.status === 200) {
return response.json();
} else {
throw new Error('Error in the request');
}
})
.then(data => {
console.log(data);
})
.catch(error => {
console.error('Error:', error);
});
Simple fetch function to call an API
Fibonacci series
export function getFibonacci(n) {
let a=0;b=1;ans=0; for(let i=2;i<=n;i++){ans=a+b; a=b;b=ans;} return ans;
}
Return the number in Fibonacci Series at index n.