Thursday, August 26, 2021

What is DOM?

 DOM stands for document object model .when a html document renders into browser it is parsed to a DOM  tree. In this DOM tree every html element becomes a object inside document object.

By using document then we can manipulate DOM elements.



What is 'this' variable?

 Regular function call:  In regular function call this variable only points at the global object (window object in browser)

Method call: 'this' variable points to the object that is calling the method.

N.B:'this' variable only assigned a value as soon as object call a method.

Ex:

calculateAge(1990);     

function calculateAge(year) {

    var age = 2021 - year;

    console.log(age);

    console.log(this);

}

output:






here 'this' is pointing to window object because the method is in global execution context .this variable is attached to global execution context.

Another example-

var john={

    name:"nabila",

    age:"28",

    qualification:"banker",

     showInfo:function(){

        console.log(this);

    }   

}

john.showInfo();

output:

{name: "nabila", age: "28", qualification: "banker", showInfo: ƒ}

here, 'this' is pointing to john object because we already know 'this' will refers to the object that called the method .

let's have another example ,

var john = {

    name: "nabila",

    age: "28",

    qualification: "banker",


    showInfo: function () {

        console.log(this);

        function innerFunction() {

            console.log(this);

        }

        innerFunction();

    }

}

john.showInfo();

output:

{name: "nabila", age: "28", qualification: "banker", showInfo: ƒ}

{window: Window, self: Window, document: document, name: "", location: Location, …}

here ,'this' inside innerFunction will point to window object however it is in the method. Because we already know in a regular function call 'this' will point to global object .

Tuesday, August 24, 2021

Creation of scope chain?

 Scope: Scope means where we can use certain variables.

In JavaScript every function creates it's own scope(Space/environment).

Lexical scoping: A function that is lexically within another function get access to the outer function .

Below is the figure of lexical scoping-





What is hoisting in javascript?

 The javascript function and variable are available in execution context before execution is know as hoisting.

Ex:

function calculateAge(year){

    console.log(2021-year);

}

calculateAge(1989);

Here we declare a function the we call it .it works fine.

but what if we call the function before define it -

calculateAge(1989);

function calculateAge(year){

    console.log(2021-year);

}

it will work too because of hoisting . In the creation phase of execution context the function declaration is stored in the variable object ,In this case global execution context .

Hoisting will not work for function expression-

Ex:

calculateAge(1989);

var calculateAge=function (year){

    console.log(2021-year);

}

it will show error.

Hoisting in variable.

console.log(age);

var age=32;

console.log(age);

output will be:

undefined

32

because in the creation phase of variable object the code is scanned for variable declaration and set them to undefined.

Monday, August 23, 2021

What is execution context?

 When a JavaScript functions call then an execution context is put on top of the execution stack. Below is the structure of a execution context object.






--Variable object(VO): All the parameters are passed to a function is stored in VO. Function declaration is stored into VO to pointing a function. Variables are scanned and for all variable a property is created in VO and set to undefined which is called hoisting .Hoisting means they are available before execution.We will know more about in the next.


How javascript works?

 





Every browser has it's own engine.ex:v8 for google chorome.  First own code hoisted into JavaScript engine and a parser parse it ,read code line by line and checks if it is correct or not .Because parser knows the JavaScript rules, If the code is syntactically correct then it parsed to a abstract syntax tree. Then the AST is converted to machine code. After converting to machine then executed my computer processor.

Sunday, August 22, 2021

Product sum using recursion.

Here we apply recursion because our array contains another array .so the process of addition is same as the outer array .

public class MainApp {

public static void main(String[] args) {

List<Object> a1 = new ArrayList<Object>();

a1.add(1);

a1.add(2);

a1.add(3);

a1.add(4);


List<Object> a2 = new ArrayList<Object>();

a2.add(5);

a2.add(6);

a2.add(7);

List<Object> arrays = new ArrayList<Object>();

arrays.addAll(a1);

arrays.add(a2);

arrays.add(8);

arrays.add(9);

int result = productSum(arrays, 1);

System.out.println(result);

}

public static int productSum(List<Object> inputs, int depth) {


int sum = 0;

for (Object obj : inputs) {

if (obj instanceof List) {

sum += productSum((List<Object>) obj, depth + 1);

} else {

sum += (int) obj;

}

}

return sum * depth;

}

}


Find frequency of character of a string using hashmap.

 public class MainApp {

public static void main(String[] args) {

String input = "aabbbcccdeefghijkkkkkk";

calculateFrequency(input);

}

private static void calculateFrequency(String input) {

Map<Character, Integer> frequency = new HashMap();

for (int i = 0; i < input.length(); i++) {

if (frequency.containsKey(input.charAt(i))) {

frequency.put(input.charAt(i), frequency.get(input.charAt(i)) + 1);

} else {

frequency.put(input.charAt(i), 1);

}

}

Set<Character> keySet = frequency.keySet();

for (Character c : keySet) {

System.out.println(c + ":" + frequency.get(c));

}

}

}


Friday, August 20, 2021

Difference between functions expression and function declaration in javascript:

 Function expression:

Expression will return a result immediately .

ex:2+3 will return 5.so this is a expression.

similarly , assume a function expression-

<script>
    var calSub = function(x, y){
        return x - y;
    }
 
    console.log("Subtraction : " + calSub(7, 4));
 </script>
here calSub is a function expression.

Function declaration:

A function is a set of statements that performs some task or does some computations and will return the result to the user.

Ex:
function calcAddition(number1, number2) 
    return number1 + number2; 
}





Thursday, August 19, 2021

Javascript object and method example.

 var johnObj = {

    calJonBMI: function () {

        this.BMI = this.mass / (this.height * this.height);

        return this.BMI;

    }

};

johnObj.fullName = "john cena";

johnObj.mass = 40;

johnObj.height = 5.6;

console.log(johnObj);

console.log(johnObj.fullName+"'s BMI is:"+johnObj.calJonBMI());


var markObj = {

    calJonBMI: function () {

        this.BMI = this.mass / (this.height * this.height);

        return this.BMI;

    }

};

markObj.fullName = "mark waugh";

markObj.mass = 45;

markObj.height = 6.5;

console.log(markObj);

console.log(markObj.fullName+"'s BMI is:"+markObj.calJonBMI());



if(johnObj.BMI>markObj.BMI){

    console.log(johnObj.fullName+" BMI is greater than "+markObj.fullName);

}

else if(markObj.BMI>johnObj.BMI){

    console.log(markObj.fullName+" BMI is greater than "+johnObj.fullName);

}

else{

     console.log(markObj.fullName+" BMI and"+johnObj.fullName+" are equal");

}

Monday, August 9, 2021

Subsequence of string using recursion.

Subsequence : If abc is a string then subsequence are - a,ab,abc,ac,b,bc,c,"".So lets find out first how we can divide this problem in suproblem .think if we split first character then the string becomes "bc".then if we find out the sequence of bc it becomes-b,bc,c,"".Now lets concatenate first splitted character "a" with the result of sequence bc ,then we will get our desired output .

  public class App {

      public static void main(String[] args) {

String word = "bc";

String subSequence = getSubSequence(word);

System.out.println(subSequence);

}


private static String getSubSequence(String word) {


if (word.isEmpty()) {

return "";

}

char first = word.charAt(0);

System.out.println(first);

String rest = getSubSequence(word.substring(1));


String result = "";

for (String subSeq : rest.split(",", -1)) {

result += "," + first + subSeq;

result += "," + subSeq;

}

return result.substring(1);

}

}

Fluent interface pattern

 public class UserConfigurationManager {     private String userName;     private String password;     private UserConfigurationManager() { ...