Skip to content

Instantly share code, notes, and snippets.

View AnkitMaheshwariIn's full-sized avatar

Ankit Maheshwari AnkitMaheshwariIn

View GitHub Profile
@AnkitMaheshwariIn
AnkitMaheshwariIn / closure-in-JavaScript.js
Created August 8, 2021 12:01
What is Closure in JavaScript? Write an example code.
// declare a global variable
var globalVar = "abc";
// self invoking parent/outer function
(function outerFunction (outerArg) { // begin scope of outerFunction
// declare a variable in outerFunction scope
var outerFuncVar = 'x';
// self-invoking closure function - inner function
(function innerFunction (innerArg) { // begin of scope innerFunction
// variable declared in innerFunction scope
@AnkitMaheshwariIn
AnkitMaheshwariIn / this-keyword-javaScript.js
Created August 8, 2021 09:31
Write code to explain this keyword?
// declare a function
function foo() {
// this refers to the reference over which this function is calling
console.log( this.bar );
}
// declare a global variable bar
var bar = "global";
// declare an object with 2 key-value
@AnkitMaheshwariIn
AnkitMaheshwariIn / private-counter-javaScript.js
Created August 8, 2021 07:38
Use a closure to create a private counter.
// declare a conterFunc
function counterFunc() {
// declare a private var
var counterVar = 0;
// return an object, with functions add & get that allows us
// to modify the private variable `counterVar`
return {
add: function(increment) { counterVar += increment; },
get: function() { return `The value of counterVar is ${counterVar}` }
}
@AnkitMaheshwariIn
AnkitMaheshwariIn / isAnagram-javaScript.js
Last active August 7, 2021 12:49
Given two strings, return true if they are anagrams of one another.
// Given two strings
var firstWord = "Mary";
var secondWord = "Army";
// call a function to check if given two words are Anagrams
isAnagram(firstWord, secondWord); // Output is: true
function isAnagram(first, second) {
// For case insensitivity, change both words to lowercase.
var a = first.toLowerCase();
// iterate for-loop 100 times
for (let i = 1; i <= 100; i++) {
// check if `i` is isFizz - that is multiples of 3
const isFizz = i % 3 == 0;
// check if `i` is isBuzz - that is multiples of 5
const isBuzz = i % 5 == 0;
// print output: if it's isFizz there could be two possibility 'Fizz' or 'FizzBizz' and
// if it's not isFizz then it will be 'isBuzz' or nothing
console.log(isFizz ? (isBuzz ? 'FizzBuzz' : 'Fizz') : isBuzz ? 'Buzz' : i);
@AnkitMaheshwariIn
AnkitMaheshwariIn / duplicate-an-array-javaScript.js
Created August 7, 2021 11:55
Duplicate an array in JavaScript
function duplicate(arr) {
// to duplicate an array use concat an array
return arr.concat(arr);
}
// call a function to duplicate array
const output = duplicate([1, 2, 3, 4, 5]);
console.log(output) // Output: [1,2,3,4,5,1,2,3,4,5]
@AnkitMaheshwariIn
AnkitMaheshwariIn / number-is-an-integer-javaScript.js
Created August 7, 2021 11:37
How would you check if a number is an integer?
function isInt(num) {
// check if remainder left when we divide by 1
return num % 1 === 0;
}
// call isInt function
console.log(isInt(4)); // true
console.log(isInt(12.2)); // false
console.log(isInt(0.3)); // false
@AnkitMaheshwariIn
AnkitMaheshwariIn / empty-an-array-2-in-javaScript.js
Created August 7, 2021 11:26
How to empty an array in JavaScript?
var arrayList = ['a', 'b', 'c', 'd', 'e', 'f']; // Created array
var anotherArrayList = arrayList; // Referenced arrayList by another variable
arrayList.length = 0; // Empty the array by setting length to 0, this will empty all reference
console.log(anotherArrayList); // Output []
@AnkitMaheshwariIn
AnkitMaheshwariIn / empty-an-array-in-javaScript.js
Created August 7, 2021 11:21
How to empty an array in JavaScript?
var arrayList = ['a', 'b', 'c', 'd', 'e', 'f']; // Created an array
var anotherArrayList = arrayList; // Referenced arrayList by another variable
arrayList = []; // Empty an arrayList
console.log(anotherArrayList); // anotherArrayList remains same, that is still pointing to old reference
// Output ['a', 'b', 'c', 'd', 'e', 'f']
@AnkitMaheshwariIn
AnkitMaheshwariIn / isArray.js
Last active August 7, 2021 11:09
Write some code to check if an object is an array or not.
const arrayList = [1 , 2, 3];
// check if arrayList is an Array or not
const isArr = Array.isArray(arrayList);
console.log(`Input arrayList is Array: ${isArr}`)