Technical Java script programs

https://www.fullstacktutorials.com/interviews/javascript-coding-questions-answers-6.html 

console.log(typeof undefined); undefined

console.log(typeof null); Object console.log(typeof NULL); undefined

console.log(typeof typeof 1); String

 Find Factorial

let n=5;
let fact=1;
for(let i=1;i<=n;i++)
{
  fact*=i;  
}

console.log("Factorial of",n,"is",fact);

Sum of given number

let n=5;
let sum=1;
for(let i=1;i<=n;i++)
{
  sum+=i;  
}

console.log("Sum of",n,"digit is",sum);


Reverse of given number


let n= 123;
let rev=0;
console.log("reverse of",n);
while(n!=0)
{
  
  rev=rev*10+n%10;
  n=parseInt(n/10);
 
}

console.log("is",rev);

Fibonacci Series of given number


let n1=0;n2=1;n3=0;
let num=10;
console.log("Fibonacci Series is :\n",n1,"\n",n2);
for(i=1;i<=num;i++)
{
  
  n3=n1+n2;
  n1=n2;
  n2=n3;
  console.log(n3);
  
}

Check given number is prime number or not
 Prime number is a number that is greater than 1 and divided by 1 or itself.

let flag=0;
let num=13;
let m=parseInt(num/2);
for(i=2;i<=m;i++)
{
if(num%i==0)
{
  console.log("Not a prime number");
  flag=1;
  break;
}
  
}
if(flag==0)
{
    console.log(num," is a prime number");
}



Check given number is Armstrong number or not
Armstrong number is a number that is equal to the sum of cubes of its digits

let num=153;
let sum=0;
let n=num;
while(n!=0)
{
  
  let mod=n%10;
  sum+=(mod*mod*mod);
  n=parseInt(n/10);
}

if(num==sum)
{
    console.log(num," is an armstrong number");
}
else
{
     console.log(num," is not an armstrong number");
}



Check number is Palindrome or not
Palindrome number in c: A palindrome number is a number that is same after reverse. For example 121,

let n= 123;
let rev=0;
let temp=n;
console.log("reverse of",n);
while(n!=0)
{
  
  rev=rev*10+n%10;
  n=parseInt(n/10);
 
}

console.log("is",rev);

if(temp==rev)
{
console.log("palindrome number");
}
else
{
console.log("not palindrome number");
}


Swap two numbers without a third variable

let a= 10;
let b=20;
 a=a+b;
 b=a-b;
 a=a-b;

console.log("a=",a,"b=",b);

Check string is Palindrome or not

let str="madam"
let flag=0;
let len=str.length;

for(let i=0;i<len;i++)
{
  if(str[i]!=str[len-i-1])
  {
    flag=1;
    break;
  }
  
}
if(flag==0)
console.log("string is Palidrom ");
else
console.log("string is not Palidrom ");

Check given number is perfect or not

 

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself.
6 is a positive number and its divisor is 1,2,3 and 6 itself.

1+2+3 = 6 which is equal to number itself.

It means 6 is a Perfect Number.

let num=6;
let sum=0;
let n=parseInt(num/2);

for(let i=1;i<=n;i++)
{
  
  mod=num%i;
  if(mod==0)
  {
    sum+=i;
  }
}

if(num==sum)
console.log("number is perfect");
else
console.log("not a perfect number");


Java Script coding practice
Reverse string without in built function

var str="test"
var rstr = "";
  for(var i = str.length - 1; i >= 0; i--){
    rstr += str.charAt(i);
  }

console.log(rstr);

let vs var  in for loop  and set timeout
const a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; for (let i = 0; i < 10; i++) { setTimeout(() => console.log(a[i]), 1000); } for (var i = 0; i < 10; i++) { setTimeout(() => console.log(a[i]), 1000); }

1 2 3 4 5 6 7 8 9 10 undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined



This is because of hoisting. Declaring a variable with var in your for loop doesn’t limit its scope to just that block of code like it would using let. Since its existence extends beyond that for loop, when the value is evaluated by the setTimeout() callback, you get the last value it was set to.

We now have a let declaration in the for loop! This changes the for loop’s behavior and thus now, each time loop is executed, a new scope is created and, for that particular scope, the callback function.

What will be the output of following js code snippet?

const number = undefined + 11;

console.log(number);

Output :  
NaN


const number = undefined + 11;
 
if (number === NaN) {
  console.log("NaN");
} else if (number === 11) {
  console.log("11");
} else {
  console.log("other");
}


Output :  
other



Comments

Popular posts from this blog

Chat Bot

Entity Framework

Microsoft Enterprise Library-Data Access Application Block for for .Net Core & .Net Standard