find value from decimal value in javascript

Find max value in a decimal value in javascript.

function Solution(n){
  var newN = n.toFixed(2).toString().split('.');
  var final = '';
  if(newN.length > 1){
    if(newN[0] > newN[1]){
      final = newN[0];
    } else {
      final = newN[1];
    }
  } else {
    final = newN[0];
  }
  return parseInt(final);
}

how to use filter in array in Javascript

In this case we will be return a value from array and that value will be above 18 and below 60.

function Solution(n){
  var newB = [];
  n.filter((item) => {
    if(item > 17 && item < 61){
      newB.push(item);
      }
  })
  return newB.toString();
}

Module Design Pattern in Javascript

var myNamespace = window.myNamespace || {};

myNamespace.module = (function(){
  //private memebers
  var privateProperty1 = false;
  var privateProperty2 = [1,2,3];
  function privateMethod1(){
    console.log('Hi');
  }
  function privateMethod2(){
    console.log('Hello');
  }

  //return object
  return{
    //public members
    publicProperty1: true,
    publicProperty2: 10,
    publicMethod1: function(){
      console.log(privateProperty1);
    },
    publicMethod2: function(){
      console.log(privateProperty2);
    },
    publicMethod3: function(){
      privateMethod1();
    },
    publicMethod4: function(){
      privateMethod2();
    }
  }
})();

class in ES6

–JS File

class Greeter {
	constructor(message){
		this.message = message;
    }
	greet(){
		return "Hello " + this.message;
    }
}

var great = new Greeter("world")

great.greet()

Class in ES5

–JS File

var Greet = (function(){
	function Greet(message){
		this.message = message;
    }
	Greet.prototype.greet = function(){
		return "Hello " + this.message;
    }
})();

var great = new Greeter("world")

great.greet()

check arguments and closure in a function in javascript

–JS File

function solution(para) {
  if(arguments.length == 0){
    return 'Please pass a argument';
  } else if(arguments.length > 1){
    var finalN = 0;
    for(var i=0; i<arguments.length; i++){
      finalN += arguments[i];
    }
    return finalN;
  } else {
    return function(para2){
      return para + para2;
    }
  }
}

Only closure

function Solution(n) {
   var myFun = function(x) {
      return add(n + (x === undefined ? 0 : x));
   };
   myFun.valueOf = function () {
      return n;
   };
   return myFun;
}

Reverse number without using string in Javascript

–JS File

function rsNumber(number){
	var finalN = 0;
	while(number > 0){
		finalN = (finalN * 10) + (number % 10);
		number = Math.floor(number/10);
	}
	return finalN;
}

Polymorphism in Javascript

Function that have multiple function and we can achieve with our input that call polymorphism

Abstraction in Javascript

Show necessary things only.

–JS file

function showOnly(){
	var name = 'show only';
	console.log(name);
	function hideonly(){
		var name2 = 'hide only';
		console.log(name2);
    }
}

Encapsulation in JavaScript

It’s a part of OOJS

Use Block scope for skipping namespace. Like we have a variable of same name that we don’t know it is already created before for that we used block scoping.

–JS File

function myName(){
   var name = 'Pradeep';
   return name;
}
function mynewName(){
   var name = 'Pradeep Kumar';
   return name;
}

myName()
mynewName()