Tuesday, December 6, 2016

Cloud Labels

framework, mvc, AngularJS, angular, angular2, angular.js, react, jsx, transformer, view, Node.js Foundation

JavaScript DOM Manipulation - jQuery - CSS - Q&A

Question:

Write code that shows how to give a class to all X tag elements in an HTML page using CSS | JS | JQUERY

Answer

// CSS

div{

    background: rgba(0, 180, 0, .3);

    width: 150px;

    height: 150px; }


// JavaScript

var divs = document.getElementsByTagName('DIV');

for (i in divs){

    divs[i].className = "newDivClass";

};


// jQuery

var divs = $('DIV');

$.each(divs, function(index){

    divs[index].className = "newDivClass"

});


JavaScript loop through array of objects - Object Array Iteration - Q&A

Question:

Show different ways to iterate over an array of objects?

Answer

var arr = [{"name":"ofer", "age":21},{"name":"ofer", "age":22},{"name":"ofer", "age":23},{"name":"ofer", "age":24},{"name":"ofer", "age":25}];

console.log("forIn:\n"); // For..In Loop
for(var i in arr)
   
console.log(arr[i].name + " " + arr[i].age);

console.log("foreach:\n"); //Foreach Loop; does not support break; Use every() instead;
arr.forEach(function(item){
   
console.log(item.name + " " + item.age);
});

console.log("for:\n"); // For Loop
for(var i = 0; i < arr.length; i++)
   
console.log(arr[i].name + " " + arr[i].age);

console.log("every:\n"); // Every Loop
arr.every(function(item){
   
console.log(item.name + " " + item.age);
   
return true
});

console.log("some:\n"); // Some Loop
arr.some(function(item){
   
console.log(item.name + " " + item.age);
});


JavaScript Array - 2 Dimensional Array - Q&A

Question:

How do you create an array in JS?
Can you create 2 dimensional array?

Answer

var arr = [1,2,3,4,5,6]; // Define simple array

var arr2d = [[1,2],[3,4],[5,6]]; // Define 2D array



arr[1] = 22; // Push data to simple array

arr2d[0][1] = 22; // Push data to 2D array



alert(arr[1]); // Read data from simple array

alert(arr2d[0][1]); // Read data from 2D array



JavaScript Session - HTML Local Storage - Q&A

Question:

What is a session and how does it works?

Answer

The sessionStorage is used for storing data on the client. The sessionStorage object is equal to the localStorage object, except that it stores the data for only one session. The data is deleted when the user closes the specific browser tab. For example;

if (sessionStorage.clickcount) {

    sessionStorage.clickcount = Number(sessionStorage.clickcount) + 1;

} else {

    sessionStorage.clickcount = 1;

}

JavaScript Session vs Cookie - Q&A

Question:

What are the differences between SESSION and COOKIE?
Where each of them is located?
Is it possible for another site to see others cookie?

Answer

In terms of capabilities, cookies only allow you to store strings. sessionStorage allow you to store JavaScript primitives but not Objects or Arrays (it is possible to JSON serialise them to store them using the APIs). Session storage will generally allow you to store any primitives or objects supported by your Server Side language/framework.
Cookies location.
Sessionstorage & localstorage location.
A properly designed browser will not allow a website to access another website's cookies, as this would violate the cross-domain policy and be a major security issue.

JavaScript - document.ready() - Q&A

Question:

What is the difference between these two lines of code?


$(document).ready(function() {}); // first line

$(function() {}); // second line


Answer


The two lines do exactly the same. The second line is just a shorten way for the first line.
 Both set the method for document ready.