Skip to content

Instantly share code, notes, and snippets.

View anil-pace's full-sized avatar

Anil kumar anil-pace

View GitHub Profile
// non-functional approach
const nums = [1, 2, 3]
let total = 0
for (let i = 0; i < nums.length; i++) {
total += nums[i]
}
console.log(total) // => 6
// functional recursive approach
@anil-pace
anil-pace / 1.js
Created September 13, 2020 10:14
let array = [1, 2, 3, 4, 5, 1];
# filter unique values
const newArray = [...new Set(array)];
// Result: [1,2,3,4,5]
# convert to boolean
const iTrue = !0;
const alsoFalse = !!0;
// Result: true
<div id="counter"></div>
<script>
var n = localStorage.getItem('on_load_counter');
if (n === null) {
n = 0;
}
n++;
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
isLoggedIn: false
}
}
toggleAuth = () => {
function useCustomHooks() {
let userName = "Anil Kumar";
let userDesignation = "Coder";
return [userName, userDesignation];
}
function ApplicationComponent() {
const [name, designation] = useCustomHooks();
return (
<div>
.parent{
position: relative;
height: 100px; # 100vh
width: 100px; # 100vw
border: 1px solid red;
}
.child{
position: absolute;
border: 1px solid green;
var calculator = {
result: 0,
one: function () {
this.result = this.result + 1;
return this;
},
three: function () {
this.result = this.result + 3;
return this;
},
@anil-pace
anil-pace / chaining.js
Created August 9, 2020 03:41
Chaining in JS
var obj = {
result: 0,
addNumber: function (a, b) {
this.result = a + b;
return this;
},
multiplyNumber: function (a) {
this.result = this.result * a;
return this;
},
@anil-pace
anil-pace / fetch.js
Last active February 26, 2022 03:53
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button id="myBtn">fetch it</button>
<script>
@anil-pace
anil-pace / rearrange_negative_positive.js
Created July 25, 2020 14:23
Rearrange negative numbers on left end and positive numbers on right most end in an array.
function rearrange_pos_negative(arr) {
var i, j;
j = 0;
for (i = 0; i < arr.length; i++) {
if (arr[i] < 0) {
if (i != j) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}