Remembering The Murder Of Diane Sindall

I remember the day vividly. It was September 1986, I had just returned to high school after the summer holidays, and we had been called into the main hall for a special assembly. We all knew what it…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Some beginners coding for interview preparation

function isPrime(n){

var divisor = 3;

limit = Math.sqrt(n);

if (n==2||n==3){

return true;

}

if (n%2==0){

return true;

}

while(divisor<=limit){

if (n%divisor==0){

return false;

}

else{

divisor += 2;

}

}

return true;

}

if(isPrime(a=237)){

console.log(a+” is Prime”);

}

else{

console.log(a+” is not prime”);

}

The following recursive code will cause a stack overflow if the array list is too large. How can you fix this and still retain the recursive pattern?

var list = readHugeList();

var nextListItem = function() {

var item = list.pop();

if (item) {

// process the list item…

nextListItem();

}

};

The potential stack overflow can be avoided by modifying the nextListItem function as follows:

var list = readHugeList();

var nextListItem = function() {

var item = list.pop();

if (item) {

// process the list item…

setTimeout( nextListItem, 0);

}

};

The stack overflow is eliminated because the event loop handles the recursion, not the call stack. When nextListItem runs, if item is not null, the timeout function (nextListItem) is pushed to the event queue and the function exits, thereby leaving the call stack clear. When the event queue runs its timed-out event, the next item is processed and a timer is set to again invoke nextListItem. Accordingly, the method is processed from start to finish without a direct recursive call, so the call stack remains clear, regardless of the number of iterations.

What is a “closure” in JavaScript?

A closure is an inner function that has access to the variables in the outer (enclosing) function’s scope chain. The closure has access to variables in three scopes; specifically: (1) variable in its own scope, (2) variables in the enclosing function’s scope, and (3) global variables. Here is an example:

var globalVar = “xyz”;

(function outerFunc(outerArg) {

var outerVar = ‘a’;

(function innerFunc(innerArg) {

var innerVar = ‘b’;

console.log(

“outerArg = “ + outerArg + “\n” +

“innerArg = “ + innerArg + “\n” +

“outerVar = “ + outerVar + “\n” +

“innerVar = “ + innerVar + “\n” +

“globalVar = “ + globalVar);

})(456);

})(123);

How will you verify a word as palindrome?

if we reverse a word and it becomes same as the previous word, it is called palindrome.

function isPalindrome(str){

var i, len = str.length;

for(i =0; i<len/2; i++){

if (str[i]!== str[len -1 -i])

return false;

}

return true;

}

> isPalindrome(‘madam’)

= true

> isPalindrome(‘toyota’)

= false

What are some of the features of ES6?

Some of the new features of ES6 are:

· Support for constants (also known as “immutable variables”)

· Block-Scope support for both variables, constants, functions

· Arrow functions

· Extended Parameter Handling

· Template Literals and Extended Literals

· Enhanced Regular Expression

· Destructuring Assignment

· Modules, Classes, Iterators, Generators

· Enhanced Object Properties

· Support for Map/Set & WeakMap/WeakSet

· Promises, Meta-Programming, Internationalization and Localization

What are let and const? And how it differs from var?

When we declare any variable using var, it was function scoped. Meaning the variable can be accessed within the function. This leads to wrap the code in a function whenever we need to create a new scope. But let and const uses block scoping. This means the variable declared using these keywords only exist within the innermost block that surronds them.

Example:

let a=5;

{

let a=3;

console.log(‘Inner a:’,a);

}

console.log(‘Outer a:’,a);

Unlike const, const is immutable. It means the value must be given at the time of the declaration and it can not be re-assigned or changes. Although we can not change the value of the const but we can mutate them.

Example:

const a=0;

a=1;

const n=[1,2,3];

b.push(3);

b[3]=4;

It is always a good practice to use let and const over var and if we are not changing the value of the variable we should use const.

What are all the new changes in Object literal notations in ES6?

ES6 allows declaring object literals by providing shorthand syntax for initializing properties from variables and defining function methods. It also enables the ability to have computed property keys in an object literal definition.

What is the drawback of creating true private methods in JavaScript?

One of the drawbacks of creating true private methods in JavaScript is that they are very memory-inefficient, as a new copy of the method would be created for each instance.

Example:

var Employee = function (name, company, salary) {

this.name = name || “”;

this.company = company || “”;

this.salary = salary || 5000;

// Private method

var increaseSalary = function () {

this.salary = this.salary + 1000;

};

// Public method

this.dispalyIncreasedSalary = function() {

increaseSlary();

console.log(this.salary);

};

};

var emp1 = new Employee(“John”,”Pluto”,3000);

var emp2 = new Employee(“Merry”,”Pluto”,2000);

var emp3 = new Employee(“Ren”,”Pluto”,2500);

Here each instance variable emp1, emp2, emp3 has its own copy of the increaseSalary private method. So, as a recommendation, don’t use private methods unless it’s necessary.

What is the difference between the function declarations below?

var foo = function(){

// Some code

};

function bar(){

// Some code

};

The main difference is the function foo is defined at run-time whereas function bar is defined at parse time. To understand this in better way, let’s take a look at the code below:

Run-Time function declaration

<script>

foo(); // Calling foo function here will give an Error

var foo = function(){

console.log(“Hi I am inside Foo”);

};

</script>

<script>

Parse-Time function declaration

bar(); // Calling foo function will not give an Error

function bar(){

console.log(“Hi I am inside Foo”);

};

</script>

Another advantage of this first-one way of declaration is that you can declare functions based on certain conditions.

What is the instanceof operator in JavaScript? What would be the output of the code below?

function foo(){

return foo;

}

new foo() instanceof foo;

instanceof operator checks the current object and returns true if the object is of the specified type.

var dog = new Animal();

dog instanceof Animal // Output : true

Here dog instanceof Animal is true since dog inherits from Animal.prototype.

var name = new String(“xyz”);

name instanceof String // Output : true

Here name instanceof String is true since dog inherits from String.prototype.

function foo(){

return foo;

}

new foo() instanceof foo;

Add a comment

Related posts:

Unocoin has listed the Dogecoin Crypto Asset

Having an unlimited supply, Dogecoin has a fast transaction rate with a low fee.

Audio Story

Before hitting the frat party Trey’s best friend, Antonio, had a really bad feeling in the pit of his stomach. While at the party Trey wants to impress his crush, so he starts taking intense shots of…

Keeping It Simple in the New Year

This seems like obvious advice — maybe too obvious; but for some writers, it seems harder in practice. We might attribute this to the fact that many of us fill our lives with nonsense; much of what…