Top 5 Basic Programming Question for interview purpose in JS.

Q1. Write a program in JavaScript that accept two integers and display the larger.

Ans.

code :-

var num1,num2;

num1= window.prompt("enter the value of num1");
num2= window.prompt("enter the value of num2");

if(num1>num2)
{
     console.log("num1 is greater");
}
else if( num2>num1)
{
    console.log("num2 is greater");
}
else
{
    console.log("both are equal");
}

Q2. Write a JavaScript for loop that will iterate from 0 to 15. For each iteration, it will check if the current number is odd or even, and display a message to the screen.

Ans.
code :-


for(var x=0; x<=15; x++)
{
    if(x===0)
    {
        console.log(x +"is even ");
    }
    else if(x%2===0)
    {
        console.log(x +"is even");

    }
    else{
        console.log(x +"is odd");
    }
}

Q3. Write a JavaScript program to construct the following pattern, using a nested for loop.

*  
* *  
* * *  
* * * *  
* * * * *  

Ans.  
     code :-
var x,y,r;
for(x=1; x <=6; x++)
{
   for (y=1; y < x; y++)
     {
       r=r+("*");        
      }
 console.log(r);
  r='';    
}

Q4.  Write a JavaScript program to compute the greatest common divisor (GCD) of two positive integers.

Ans.

Code :-

var a=7666;
var b=54;
var gcd;
while(a!=b)
{
    if(a>b)
    {
        a=a-b;
    }
    else
    {
       b=b-a;
    }
}
gcd =a;
console.log(gcd);

Q5. Write a JavaScript conditional statement to sort three numbers. Display an alert box to show the result. 

Ans.

Code :-

var x=0, y=-1 , z=4;
if(x>y && x>z)
{
    if(y>z)
    {
        
        console.log(x+","+y+","+z );
    }
    else
    {
        console.log(x+","+z+","+y );
    }
}

else if(y>x && y>z)
{
    if(x>z)
    {
        
        console.log(y+","+x+","+z );
    }
    else
    {
        console.log(y+","+z+","+x );
    }
}

else if(z>x && z>y)
{
    if(y>x)
    {
        
        console.log(z+","+y+","+x );
    }
    else
    {
        console.log(z+","+x+","+y );
    }

Tanku

Related Posts

Modern DataOps Infrastructure: Unlocking the Power of Observability Platforms

Introduction Modern enterprise data architectures are growing increasingly complex. Today, an ordinary business analytics pipeline might ingest streaming IoT logs, batch-load transactional customer databases, transform those layers…

Read More

Elevating DevSecOps and SRE Efficiency with a Software Delivery Governance Platform

Introduction Enterprise software engineering has reached a tipping point where systemic complexity threatens structural delivery stability. Modern engineering organizations routinely support highly fragmented ecosystems populated by hundreds…

Read More

Best Hospitals in India for International Patients and Affordable Surgery Costs

Introduction Global healthcare costs are rising rapidly, forcing many families to look for alternative solutions when facing serious medical diagnoses. In countries like the United States, the…

Read More

A Beginner Guide to Data Analytics Automation using Enterprise DataOps Workflows

Organizations rely heavily on fast, accurate, and reliable business intelligence to make critical commercial decisions. Whether it is predicting customer churn or managing real-time inventory levels, business…

Read More

Integrating AI Tools in DataOps Pipelines: A Comprehensive Guide

Introduction Modern organizations deal with a massive influx of data from applications, IoT devices, and cloud services. Managing these data volumes requires speed, accuracy, and agility. Traditional…

Read More

Modern Cloud DataOps Platforms for Reliable Data Pipelines

Introduction Modern organizations depend heavily on data. Every department, from finance and sales to healthcare, manufacturing, marketing, and customer support, needs reliable data to make better decisions….

Read More