Skip to content

Instantly share code, notes, and snippets.

@Code-Crash
Last active April 27, 2021 09:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Code-Crash/ae2ee8ac6605ac973bf869d872d3b45a to your computer and use it in GitHub Desktop.
Save Code-Crash/ae2ee8ac6605ac973bf869d872d3b45a to your computer and use it in GitHub Desktop.
/**
* @author code-crash
* @description - This file contains the basic question and solutions for the JavaScript language
*
*/
/**
* @type - 1
* @description - Make array 4000 of each 4000 array, tell the three approaches.
*
* @default - at list one solution should be only in single line of code
*
*/
var size = 4000;
// Default Solution: -
new Array(size).fill().map(function (_i, index, array) { return new Array(size).fill().map(function (i, index) { return index; }) });
/**
* @type - 2
* @description - Convert String/Boolean to Numbers and tell the output for follows and explain why?
*
* @default - Try to convert and explain the output.
*
* @code - parseInt('11'); // what output ?
* @code - parseFloat('22.22'); // what output ?
* @code - Number(true); // what output ?
* @code - Number(false); // what output ?
* @code - Number(null); // what output ? // Null Object to number
* @code - Number('ss'); // what output ? // Chars to number
* @code - Number(undefined); // what output ? // Undefined value to Number
*
* @default - Copy/Paste these lines in browser console or node.js console to see the results for better understanding
*
*/
/**
* @type - 3
* @description - What is the different between `==` and `===`, and where we can utilize this?
*
* @default - Explain the difference and try to explain the scenarios where you can use it.
*
* @see - `==` check only for equality of the value and `===` check both equality and type
* @example - Suppose we got the scenario like we need to compare two values '1' and 1 as below:
*
* @code - console.log(1 == 1); // expected output: true
* @code - console.log("1" == 1); // expected output: true
* @code - console.log(1 === 1); // expected output: true
* @code - console.log("1" === 1); // expected output: false
*
* @readonly - The '1' is string and 1 is a number,
* - we can see clearly in above example that we are comparing
* - the '1' == 1 (value comparing ) and '1' === 1 (value and type comparing)
*
*/
/**
* @type - 4
* @description - What is the different between null and undefined?
*
*
* @see - The typeof(null) is an object and the typeof(undefined) is an undefined,
* - So we can see the null is an object but the value is null and the undefined is an undefined value.
*
* @example - Some times we get the scenarios to execute some code base on typeof as below:
*
* @code - if(typeof _var === 'object') { // execute code }
*
* @see - On above code, there is a fault, suppose we can the _var = null and null is not actually a value,
* - but in avode condition, out if condition will work as true,
* - so to avoid that situation we can add some extra code as below:
*
* @code - if(typeof _var === 'object' && _var != null) { // execute code }
*
* @external - typeof keyword is an Unary Operator, which is used to find out the operand type
*/
/**
* @type - 5
* @description - Write a code by using closure to print the incremental count by 1,
* - without using any global variable except parent closure's parent function
*
* @example - When we call that count() function, it should return the follows responses:
*
* @code - count() // first time calling, expected output: 1
* @code - count() // second time calling, expected output: 2
* @code - count() // third time calling, expected output: 3
* @code - count() // fourth time calling, expected output: 4 ... like wise
*
* @see - Solution is as below:
*
* @code - var count = function() {
* var _v = 0;
* return function() { return _v = _v + 1; };
* }();
*
* @see - To understand the code, you can put a proper variable and make. `return` more clear after `adding` the +1 in _v, (like _v = _v + 1; return _v;)
*/
/**
* @type - 6
* @description - What's a difference between 'this' and 'self' in below example, explain and provide the output also.
*
* @code - var _obj = {
* key: 'value',
* _fun: function() {
* var self = this; // In Object, immediate function's `this`, will always reference to that object, in this case it's `_obj`.
* console.log('output 1:', this.key);
* console.log('output 2:', self.key);
* (function() {
* console.log('output 3:', this.key); // In Function's, immediate `this`, will always reference to the function it self.
* console.log('output 4:', self.key);
* }());
* }, // _fun closed
* }; // _obj closed
* _obj._fun();
*
* @default - // Expected output will be:
* output 1: value // As `this` is referring to current object
* output 2: value // `self` is pointing to `this` and `this` is pointing to _obj
* output 3: undefined // `this` is pointing to immediate anonymous function and anonymous function don't have any `key` variable
* output 4: value // `self` is pointing to `this` of outer function (global function), so for anonymous function, `self` is global variable
*
* @see - We can see that the, `this` will get changed according to the scope, so as we can say,
* - `this` is a static for the current instance of an immediate object or a immediate functions
*/
/**
* @type - 7
* @description - Clear a Inheritance in ES5 without using prototype with example
*/
var Person = function () {
this.getName = function () {
return 'parent';
}
};
var Employee = function (org, name) {
this.getOrg = function () {
return org;
}
}
function util(Person, Employee) {
var temp = Employee.prototype.constructor;
Employee.prototype = new Person();
Employee.prototype.constructor = temp;
}
util(Person, Employee);
console.log(new Employee('arup').getName());
//console.log(new Person('arup').getName());
/**
* @type - 8
* @description - From Array 1-20 or 1-n, get total of even number and total of odd numbers using only filter, map and reduce.
* @param {*} array
*
* @example
* @see new Array(0, 1, null, undefined, 2, true , false).filter(function(a){ return a; }); // Filter work on conditions, in this case its simple returning if (a) is valid so that's why 0, null, false and undefined getting remove
* @see new Array(1, 2, 3, 4, 5, 6, 7).filter(function(a){ return a % 2 == 0; }); // condition for event number, for odd numbers change a % 2 != 0
* @see new Array(1, 2).map(function(a, v){ return a / 2 ; }); // Map will override the current index based on return conditions
* @see new Array(1, 2).reduce(function(a, v){ return a / v; }); // Reduce will manipulate/store the values in accumulator based on return or assignment.
* @see new Array(1, 2).reduceRight(function(a, v){ return a / v; });
*/
function EvenOdd (array) {
var eTotal = array.filter(function(item) { return item % 2 == 0; });
var oTotal = array.filter(function(item) { return item % 2 !== 0; });
eTotal = eTotal.reduce(function (_a, _cV) { return _a + _cV});
oTotal = oTotal.reduce(function (_a, _cV) { return _a + _cV});
return { eTotal : eTotal, oTotal: oTotal};
}
EvenOdd([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
// 9. Find the missing element from the Integer array (one or more missing number)
let arr = [1, 2, 4, 20, 6, 8, 10];
arr.sort((a, b) => a - b); // If array arr is not in sorted order, we can uncomment this.
let low = arr[0];
let high = arr[arr.length - 1];
let missing = [];
for (let i = low; i <= high; i++){
if (arr.indexOf(i) == -1) {
missing.push(i);
}
}
console.log('Output: ', missing); // Output: [3, 5, 7, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19]
// 9. IN JAVA
public class MissingNumbers {
static void missings(int arr[], int N){
// Initialize diff
int diff = arr[0] - 0;
for(int i = 0; i < N; i++) {
// Check if diff and arr[i]-i both are equal or not
if (arr[i] - i != diff) {
// Loop for consecutive missing elements
while (diff < arr[i] - i) {
System.out.print((i + diff) + " ");
diff++;
}
}
}
}
public static void main(String[] args) {
int arr[] = {1, 2, 4, 20, 6, 8, 10};
int N = arr.length;
missings(arr, N);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment