0% found this document useful (0 votes)
6 views

Javascript Interview Questions Example

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Javascript Interview Questions Example

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Examples

1. Closures example
https://www.youtube.com/watch?v=t1nFAMws5FI

function one(){ function onetime(){


var a =500; for(var i=0; i<=10; i++){
function two(){ setTimeout (function(){
console.log(a); console.log(i);
} }, 3000);
two(); }
} }
one(); /o/p: 500 onetime(); /o/p: 11(11 times)
—--------------------------------------------------- function onetime(){
function one(){ var i
var a =500; for(i=0; i<=10; i++){
function two(){ setTimeout (function(){
console.log(a , b);} console.log(i);
return two;} }, 3000);
⇒ one()(); }
Let oneoutput = one(“hello world”); }
oneoutput() onetime(); /o/p: 11(11times)
o/p: 500 , helloworld for(let i=0; i<=10; i++)
onetime(); /o/p: 1 2 3 4…10

function onetime(){
for(var i=0; i<=10; i++){
function sectime(i){
setTimeout (function(){
console.log(i);
}, 3000);
}
sectime(i);
}}
onetime();

2. Call,bind,apply example
let namecall = {
firstname: "ramya", secondname: "rajasekar",
thirdname : function (state,district,fruit){
console.log("this is " + this.firstname + " " + this.secondname + " " + "Reona" + " state is " +
state + " District is " + district + " and finally " + fruit); }}
namecall.thirdname("tamilnadu", "thiruvallur", "apple");
let namecallsecond ={ firstname: "rajasekar", secondname: "reona"}
namecall.thirdname.call(namecallsecond, "tamilnadu", "thiruvallur", "apple");
namecall.thirdname.apply(namecallsecond, ["tamilnadu", "thiruvallur" , "apple"]);
let printmyname = namecall.thirdname.bind(namecallsecond, "tamilnadu", "thiruvallur",
"apple");
console.log(printmyname);
printmyname();

3. Flattern the array


let arr =[ [1,3],[3,4,5],[5,6,7],[7,8,[9,10]]]
// not works// sub array console.log([].concat(...arr));
console.log(arr.flat(2));

let arr =[ [1,3],[3,4,5],[5,6,7],[7,8,[9,10]]]


function flatern(arr,depth){
let result = [];
arr.forEach((ar) => { if(Array.isArray(ar) && depth >0){
result.push(...flatern(ar,depth-1));
}
else result.push(ar);});
return result; }
console.log(flatern(arr));

4. Average:
let sample = [{name: "jagan", age: 32}, {name: "jagan", age: 23}, {name: "ven",
age: 30}];

let out = 0
for(let i =0; i< sample.length; i++){
out += sample[i].age/sample.length}
console.log(out);

let out = sample.reduce((acc,curr) => acc + curr.age/sample.length,0);


console.log(out);
String Based questions
1. Reverse a string
let ars = "hello world"
1.1. let out = function(arg){return arg.split("").reverse().join("");}
1.2. let out = function(arg){return arg.split("").reduce((acc,curr) => curr +
acc,"");}
1.3. let out = function(arg){let first = "";
for(let i=arg.length-1;i>=0; i--)
{first +=arg[i]}return first;}

Output
console.log(out(ars));
o/p: dlrow olleh

2. Remove Duplicates
let originalStr = "hello world";
2.1. let out = function(arg){return [...new Set(arg)].join("");}
2.2. let out = function(arg){return arg.split("").filter((item,index,arr)
=>arr.indexOf(item) === index).join(""); }
2.3. let out = function(arg){let outinside = "";for(let i=0;i<arg.length; i++){
if(outinside.indexOf(arg[i]) === -1){
outinside += arg[i]}}
return outinside;}

outinside.indexOf(arg[i]) === -1//


arr.indexOf(arr[i]) checks position like 0,1,2,3 it equates to
i, this return the first occurrence alone
2.4. let out = originalStr.split("").reduce((acc,curr) => {
if(!acc.includes(curr)){acc.push(curr);} return acc;}, []).join("")

Output
console.log(out(ars));
o/p: helo wrd

3. Print true for duplicate present


let ars = "hello world"
3.1. let out = function(arg){ let newarg = new Set(arg)
return newarg.size !== arg.length}
3.2. let out = function(arg){
for(let i=0; i<arr.length; i++){
if(arr.indexOf(arr[i]) !== i){return true;}}
return false}
arr.indexOf(arr[i]) !== i//
arr.indexOf(arr[i]) checks position like 0,1,2,3 it equates to
i, this return the first occurrence alone
3.3. let out = function(arg){
for(let i=0;i<arg.length; i++){
for(let j= i +1; j<arg.length; j++){
if(arg[i] == arg[j]){return true;}}}
return false;}

Output
console.log(out(ars));
o/p: true

w
Array Based questions
1. Find Min in array
let arr = [23,45,7,89,99,1,9,11];
1.1. let out = function(arg){return Math.min(...arg);}
1.2. let out = function(arg){let output = arg[0]
for(let i =0; i<arg.length; i++){
if(output > arg[i]){output = arg[i]}}
return output;}
1.3. let out = arr.reduce((acc,curr) => {return Math.min(acc,curr)})
console.log(out);
Output: console.log(out(arr)) o/p: 1

2. Find Max in array


let arr = [23,45,7,89,99,1,9,11];
2.1. let out = function(arg){return Math.max(...arg);}
2.2. let out = function(arg){let output = arg[0]
for(let i =0; i<arg.length; i++){
if(output < arg[i]){output = arg[i]}}
return output;}
2.3. let out = arr.reduce((acc,curr) => {return Math.max(acc,curr)})
console.log(out);
Output: console.log(out(arr)) o/p: 99

3. Sort an array Ascending order


let arr = [23,45,7,89,99,1,9,11];
3.1. let out = function(a,b){return a - b;}
console.log(arr.sort(out));
3.2. let out =arr.sort(a,b){return a - b;}
3.3. let out =[...arr].sort((a,b) => a - b)
console.log(out);
Output: o/p: [1, 7, 9, 11, 23, 45, 89, 99]

4. Sort an array Descending order


let arr = [23,45,7,89,99,1,9,11];
4.1. let out = function(a,b){return b - a;}
console.log(arr.sort(out));
4.2. let out =arr.sort(a,b){return b - a;}
4.3. let out =[...arr].sort((a,b) => b - a)
console.log(arr(out));
Output: o/p: [99, 89, 45, 23, 11, 9, 7, 1]

5. Remove Duplicates
let arr = [23,45,7,89,99,1,9,11,1,1,1];
5.1. let out = function(arg){return [...new Set(arg)]}
5.2. let out = function (arg) {return arg.filter((item,index,arr) =>
arr.indexOf(item) === index)}
5.3. let out = arr.reduce((acc,curr) => {if(!acc.includes(curr)){
acc.push(curr)}
return acc;}, [])

console.log(out(arr));
Output: o/p: [23, 45, 7, 89, 99, 1, 9, 11]

6. Print true for Duplicates


let arr = [23,45,7,89,99,1,9,11,1,1,1];
6.1. let out = new Set(arr).size !== arr.length
6.2. let out = function(arg){
for(let i=0;i<arg.length; i++){
for(let j= i +1; j<arg.length; j++){
if(arg[i] == arg[j]){return true;}}}
return false;}
6.3. let out = arr.filter((item,index,arr) => arr.indexOf(item) !==
index).length > 0
arr.indexOf(item) !== index//
Filtered without repetition afterwards check the length that
is greater than 0 mean duplicate is there so true.

console.log(out);
Output: o/p: true,

7. Find the element in an array


let arr = [23,45,7,89,99,1,9,11,1,2,45]
7.1. Last element in array
console.log(arr[arr.length-1]) o/p: 45
7.2. Last second element in array
console.log(arr[arr.length-2]) o/p: 2
7.3. Second Largest element in array
let out = arr.sort((a,b)=> b-a)
let secondone = arr[1]
console.log(secondone)

7. Asynchronous fetch api


Example:
const promise = new Promise((resolved,rejected) =>{
const error = true;
if(!error){
resolved("yes, Resolved the error");}
else{rejected("No, Erorr")}
})
const mynext = new Promise((resolve,reject) => {
setTimeout(function(){resolve("net promise executed...")}, 1000)
})
console.log(promise);
console.log(mynext);
promise.then(value => console.log(value))
.catch(err => console.log("error", err))
mynext.then(value => console.log(value))
.catch(err => console.log("error", err))
Output:
Promise {<pending>}
error No, Erorr
Promise {<pending>}
net promise executed...

1. Possibilities of o/p
Var Let Const

var a = 10; let a = 10; const a = 10;


function x(){ function x(){ function x(){
a = 20; console.log(a)} a = 20; console.log(a)} a = 20 console.log(a)} x()
x() console.log(a) x() console.log(a) console.log(a)
20 20 20 20 Uncaught Typeerror

Var a = 10; let a = 10; const a = 10;


function x(){ function x(){ function x(){
Var a = 20 console.log(a)} let a = 20 console.log(a)} Const a = 20 console.log(a)}
x() x() x()
console.log(a) console.log(a) console.log(a)
20 10 20 10 20 10

function sample() { function sample() { function sample() {


var a = 2; let a = 2; const a = 2;
if(true) { if(true) { if(true) {
var a = 3 let a = 3 const a = 3
console.log(a);} console.log(a);} console.log(a);}
console.log(a);} console.log(a);} console.log(a);}
33 32 32

console.log(x) console.log(x) console.log(x)


var x = 1; let x = 1; const x = 1;
undefined cant aces before initialization cant aces before initialization

You might also like