Skip to content

Instantly share code, notes, and snippets.

View gbhasha's full-sized avatar

Galeel Bhasha Satthar gbhasha

View GitHub Profile
@gbhasha
gbhasha / .zshrc
Created August 31, 2020 14:17 — forked from majgis/.zshrc
automatically call nvm use if .nvmrc is present
# Taken from here:
# https://github.com/creationix/nvm#zsh
# place this after nvm initialization!
autoload -U add-zsh-hook
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
@gbhasha
gbhasha / cmd
Created November 28, 2018 10:29
Terminal command to find top used commands in your terminal
history | awk '{print $2}' | sort | uniq -c | sort -rn | head -5
@gbhasha
gbhasha / FizzBuzz.js
Last active November 8, 2018 04:54
Write a program that prints the numbers from 1 to 100. But for multiples of three print ‘Fizz’ instead of the number and for the multiples of five print ‘Buzz’. For numbers which are multiples of both three and five print ‘FizzBuzz’.
/*
Write a program that prints the numbers from 1 to 100. But for multiples of three print ‘Fizz’ instead of the number and for the multiples of five print ‘Buzz’. For numbers which are multiples of both three and five print ‘FizzBuzz’.
*/
(function fizzBuzz() {
console.clear();
const isMutlipleOfThree = (i) => (i % 3 === 0 )
const isMutlipleOfFive = (i) => (i % 5 === 0)
@gbhasha
gbhasha / setOperations.js
Created November 4, 2018 22:36
Implementing basic set operations - isSuperset, union, intersection and difference
function isSuperset(set, subset) {
for (var elem of subset) {
if (!set.has(elem)) {
return false;
}
}
return true;
}
function union(setA, setB) {
@gbhasha
gbhasha / removeDuplicates.js
Last active November 4, 2018 22:00
4 ways to remove duplicates from an array
let a = [1,2,5,1,1,2,8];
// 1. Using for loop
let b = [];
let len = a.length;
for(let i=0; i<len; i++) {
if(b.indexOf(a[i]) === -1) {
b.push(a[i]);
}
}
@gbhasha
gbhasha / syncLocalBranches.sh
Created October 15, 2018 13:20
Removes local branches which are already removed in remote.
git fetch -p && for branch in `git branch -vv | grep ': gone]' | awk '{print $1}'`; do git branch -D $branch; done
@gbhasha
gbhasha / has-own-property.js
Created October 3, 2018 11:25 — forked from quisido/has-own-property.js
Object.prototype.hasOwnProperty.call
const myObject = Object.create(null);
myObject; // {}
myObject.test; // undefined
Object.prototype.hasOwnProperty.call(myObject, 'test'); // false
// Uncaught TypeError:
// myObject.hasOwnProperty is not a function
myObject.hasOwnProperty('test');
@gbhasha
gbhasha / firstDuplicate
Created May 3, 2018 06:20
Given an array a that contains only numbers in the range from 1 to a.length, find the first duplicate number for which the second occurrence has the minimal index. In other words, if there are more than 1 duplicated numbers, return the number for which the second occurrence has a smaller index than the second occurrence of the other number does.…
function firstDuplicate (numArray) {
const len = numArray.length;
let result = -1;
if(len < 2) {
return result
}
let dupArray=[];
@gbhasha
gbhasha / smallestPrime.js
Created April 12, 2018 09:22
Print the Smallest Prime number which is in the even position of the given array.
// const inputArr = [4,1,3,14,15,18,39,56,89,101,150,165,187] // output 3
// const inputArr = [9,31,38,5,62,44,38,17,19,38,50,74] // output 5
const inputArr = [5,6,8,9,11,14,16,18,20,25] //output 'null'
const smallestPrime = (numArray) => {
const inputArr = numArray.slice();
const isPrime = num => {
for(let i = 2, s = Math.sqrt(num); i <= s; i++)
if(num % i === 0) return false;
return num !== 1;
function is(type, object) {
type = type[0].toUpperCase() + type.slice(1);
var toString = Object.prototype.toString;
return toString.call(object) === '[object ' + type + ']';
}
console.log(is('object', {})); // true
console.log(is('array', [])); // true
console.log(is('object', [])); // false
console.log(is('number', 2)); // true
console.log(is('number', '2')); // false