Skip to content

Instantly share code, notes, and snippets.

View jw-jenrise's full-sized avatar

John Wilfred Adaikalam Selvaraj jw-jenrise

View GitHub Profile
@jw-jenrise
jw-jenrise / func_js_hoisting
Last active October 24, 2017 23:31
Function names do not support hoisting.
Function names do not support hoisting.
@jw-jenrise
jw-jenrise / Map2Mutate.java.snippet
Last active December 3, 2017 22:51
A map function that would mutate the incoming collection set boolean and return the mutated list
//
getAssociatedServicesFromOrder(orderForm)
.stream()
.map(
servie -> return service.withAdditionalFeatures(interestedServices.contains(ts.getSecondaryService()))
)
.collect(Collectors.toList()
//
// JAVASCRIPT IS DYNAMICALLY TYPED
var age = 30;
console.log("Age = " + age); // Display's age
console.log("Type of variable age = " + typeof(age)); // Type of variable
console.log("--- Dynamically typed ---")
age = "new born"; // Proof JavaScript is dynamically typed
console.log("Type of variable age = " + typeof(age)); // Type has changed
// Variables declared by default are undefined
console.log("Begin example - 2");
var x,y = 10;
console.log("Value of x = " + x); // X value is not defined, hence has undefiend
console.log("Value of y = " + y);
// Types demystified
console.log("Type of x = " + typeof(x)); // Type of undefined is undefined.
console.log("--");
console.log("Begin example - 3");
var p = 0; // Try replacing this with null, undefined, NaN
console.log("Value of p = " + p);
console.log("Boolean equivalent of p = " + Boolean(p)); // Truthy or Falsy value
// Example usage of truthy or falsy
if (p){
console.log("I am happy");
} else {
console.log("I am not sure");
}
console.log("Begin example - 4");
// Adding of three numbers using a function
// Define an add function
function add(a,b,c){
return a + b + c;
}
// Calling the function
var d = add(10,20,30);
console.log("Begin example - 5");
// Adding of three numbers using a function
// Define an add function
function add(a,b,c){
return a + b + c;
}
// EXTRA ARGUMENT IS IGNORED
var e = add(1,2,3,4);
console.log("Begin example - 6");
// Adding of three numbers using a function
// Define an add function that can work for three or less arguments
function add(a,b,c){
var d = ( (a) ? a : 0 ) + // Using ternary operator we can check the
( (b) ? b : 0 ) + // Truthy/Falsy of the parameter and then
( (c) ? c : 0 ) ; // Substitue with numeral zero so that NaN is avoided.
return d;
}
function printRemoteData(url, message){
//var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; //Uncomment this line if you are using it in node js environment.
var xhr = new XMLHttpRequest();
var delayedUrl = url + "?delay=" + (Math.floor(Math.random() * 9) + 1);
console.log(delayedUrl);
xhr.open("GET", delayedUrl, true);
xhr.onload = function(){
if (message){
console.log(message);
}
function getRemoteData(url){
//var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; //Uncomment this line if you are using it in node js environment.
// 1. Create the request
var xhr = new XMLHttpRequest();
// 2. Create the URL
var delay = Math.floor(Math.random() * 9) + 1;
// Open the URL