Skip to content

Instantly share code, notes, and snippets.

View msaxena25's full-sized avatar

Mohit Saxena msaxena25

View GitHub Profile
@msaxena25
msaxena25 / hoisting.js
Created January 15, 2019 03:38
JavaScript Hoisting Console Input Output Questions
function emp() {
console.log('111');
}
emp();
function emp() {
console.log('2');
}
Output > 2
var emp = function() {
@msaxena25
msaxena25 / Anagrams.js
Created January 4, 2019 07:34
JavaScript Anagrams program - Anagrams are words or phrases that contain the same number of characters.
function checkAnagrams(input1, input2) {
if(input1.length !== input2.length) {
return false;
}else {
var str1 = input1.toLowerCase().split('').sort().join('');
var str2 = input2.toLowerCase().split('').sort().join('');
if(str1 === str2) {
return true;
@msaxena25
msaxena25 / permutation.js
Created December 26, 2018 06:49
JavaScript Create all possible Combination string from a given Input string : Example of Permutation
// First way
function swap (alphabets, index1, index2) {
var temp = alphabets[index1];
alphabets[index1] = alphabets[index2];
alphabets[index2] = temp;
return alphabets;
}
function permute (alphabets, startIndex, endIndex) {
@msaxena25
msaxena25 / test.js
Last active January 14, 2019 08:00
JavaScript Awesome console Outputs
1. Both are same!
var obj = new Object(); //Empty Object
var obj = new Object; //same result
2. var obj = new Object(id: 1, name: "test");
Output > it will give error
3.
var obj1 = {id: 1, name: "test"};
@msaxena25
msaxena25 / gist:d6ef47bcd3b1f08724728845255f4131
Last active December 20, 2018 12:08
JavaScript - Compare two Objects are same or not Best & Simple way
function equal(obj1, obj2) {
let keys = Object.keys(obj1); // first find keys of first Object
let output = false;
for (let i = 0 ; i < keys.length ; i++) {
output = (obj2.hasOwnProperty(keys[i]) && obj1[keys[i]] == obj2[keys[i]]) ? true : false;
}
return output;
@msaxena25
msaxena25 / palindrone.js
Last active January 3, 2019 13:03
JavaScript Check Palindrome
function checkPalindrome(str) {
if(str) {
var pattern = /[^a-zA-z]+/g; // first remove spaces and special chars from given string
str = str.replace(pattern, '').toLowerCase();
var str1 = str.split('').reverse().join('');
console.log(str, str1); // for testing
if(str1 == str.toLowerCase()) {
return true;
@msaxena25
msaxena25 / test.js
Last active April 29, 2023 08:17
JavaScript Count Duplicate Characters in a String Best way
function duplicateCharCount(str) {
if(str) {
var obj = {};
for(let i = 0; i < str.length; i++) {
if(obj[str[i]]){
obj[str[i]] += obj[str[i]];
}else {
obj[str[i]] = 1;
@msaxena25
msaxena25 / app.component.html
Last active November 28, 2018 11:29
Spread sheet design in Angular 5
<div class="main">
<div class="parent-container">
<div class="row" *ngFor="let number of numberArray;let rowIndex = index;">
<div [ngClass]="{'header' : rowIndex == 0 , 'input-field' : rowIndex > 0, 'first-col' : colIndex == 0 , 'highlight' : rowIndex == selectedRowIndex || colIndex == selectedColumnIndex}"
*ngFor="let char of charArray;let colIndex = index;">
<input *ngIf="rowIndex > 0 && colIndex > 0;else headertemplate" type="text" (focus)="onInputFocus(rowIndex, colIndex)">
<ng-template #headertemplate>
{{rowIndex == 0 ? char : number}}
</ng-template>
</div>
@msaxena25
msaxena25 / app.component.html
Created November 27, 2018 13:25
Spread sheet in Angular
<div class= "main" #maindiv></div>
@msaxena25
msaxena25 / time.js
Created November 21, 2018 10:44
Convert UTC to Local time JavaScript
// To covert the Utc time to local time
convertUTCDateToLocalDate(date) {
if (date) {
let date1;
if (date.indexOf('-') > -1) {
date1 = this.splitdate(date);
}
else {
date1 = new Date(date);
}