Showing posts with label javascript answers. Show all posts
Showing posts with label javascript answers. Show all posts

Sunday, 27 March 2016

JavaScript Questions


Some question of JavaScript programs are given below as links. Click on that link which one you want to know more. The given links will display the program code and its output. Here I displayed the outputs as Images. If you want to check its working, copy that code to a Notepad or any text editors and then save as a HTML file.

Questions are given below. Click on that to get answers,
  1. Write a JavaScript program to find the area and circumference of a circle?
  2. Write a JavaScript program to show the Alert Box?
  3. Write a JavaScript program to store different colors in an array and change the background color of the page using this array elements?
  4. Write a JavaScript program for form validation?
  5. Write a JavaScript program to print first n even numbers?
  6. Write a JavaScript program to print first n prime numbers?
  7. Write a JavaScript program to print first n Fibonacci numbers?
  8. Write a JavaScript program to print prime numbers in a range?
  9. Write a JavaScript program to perform case conversion?
  10. Write a JavaScript program to alert content of text box?
  11. Write a JavaScript program to illustrate onMouseOver event?
  12. Write a JavaScript program to illustrate onKeyup event?
  13. Write a JavaScript program to print strange numbers in a range?
  14. Write a JavaScript program to print fibonacci numbers in a range?

Write a JavaScript program to find the area and circumference of a circle?

Code:


<!DOCTYPE html>
<html>
<body>
<center>
<h1>Find the area and circumference of a circle</h1>
<script>
function Calculate()
{
var radius=document.form1.txtRadius.value;
document.getElementById("demo").innerHTML="The area of the circle is "+(radius*radius*Math.PI)+"<br/>The circumference of the circle is "+(2*radius*Math.PI);
}
</script>
<form name="form1">
Enter the radius of the circle: <input type="text" name="txtRadius" size="10">
<input type="button" value="Calculate" onclick="Calculate()">
</form>
<p id="demo"></p>
</center>
</body>
</html>


Output:



Write a JavaScript program to show the Alert Box?

Code:


<!DOCTYPE html>
<html>
<body>
<p>Click the button to display an alert box</p>
<button onclick="myfunction()">
Try it
</button>
<script>
function myfunction()
{
alert("I am an alert box!");
}
</script>
</body>
</html>

Output:



Write a JavaScript program for form validation?

Code:


<!DOCTYPE html>
<html>
<script>
function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if(x == null || x == "")
{
alert("Name must be filled out");
return false;
}
var y=document.forms["myForm"]["lname"].value;
if(y == null || y == "")
{
alert("Last Name must be filled out");
return false;
}
}
</script>
<body>
<form name="myForm" onsubmit="return validateForm()">
First Name: <input type="text" name="fname"/>
Last Name: <input type="text" name="lname">
<input type="submit" value="Submit">
</form>
</body>
</html>

Output:



Write a JavaScript program to store different colors in an array and change the background color of the page using this array elements?

Code:


<!DOCTYPE html>
<html>
<body>
<center>
<script>
function myArray(n)
{
var backcolor=["#00f00","#ff0000","#0000ff","#cce"];
document.bgColor=backcolor[n];
}
</script>
<a href="#" onmouseover="myArray(1)">Red</a>
<a href="#" onmouseover="myArray(2)">Blue</a>
<a href="#" onmouseover="myArray(4)">Green</a>
<a href="#" onmouseover="myArray(3)">Ash</a>
<h1>Changing background color</h1>
</center>
</body>
</html>

Output:



Saturday, 26 March 2016

Write a JavaScript program to print first n prime numbers?

Code:


<!DOCTYPE html>
<html>
<body>
<center>
<h1>First N Prime Numbers</h1>
<script>
var nl="<br>";
var n;
var status=1;
var num=3;
n=prompt("Enter the limit");

if (n>=1)
{
document.write(2+nl);
}
for (var i = 2; i <= n;)
{
for (var j = 2; j <= Math.sqrt(num) ; j++)
{
if (num%j==0)
{
status=0;
break;
}
}
if (status!=0)
{
document.write(num+nl);
i++;
}
status=1;
num++;
}
</script>
</center>
</body>
</html>

Output:


 

Write a JavaScript program to print first n even numbers?

Code:


<!DOCTYPE html>
<html>
<body>
<center>
<h2>First N Even Numbers</h2>
<p id="demo"></p>
<script>
var i=1;
var j=prompt("Enter the limit");
var k=2*j;
var text=" ";
for (var i=1;i<=k;i++)
{
if (i%2==0)
{
text=text+i+"<br>";
}
}
document.getElementById("demo").innerHTML=text;
</script>
</center>
</body>
</html>

Output:


 

Write a JavaScript program to print first n fibonacci numbers?

Code:


<!DOCTYPE html>
<html>
<body>
<center><h1>First N Fibonacci Numbers</h1>
<script>
    var fib1=0,fib2=1,fib3,num,count=0;
    var nl="<br>";
    num=prompt("Enter the limit");
    document.write(fib1+nl);
    document.write(fib2+nl);
    count=2;
    while(count<num)
    {
        fib3=fib1+fib2;
        count++;
        document.write(fib3+nl);
        fib1=fib2;
        fib2=fib3;
    }
</script>
</center>
</body>
</html>

Output:



Write a JavaScript program to print prime numbers in a range?

Code:


<!DOCTYPE html>
<html>
<body>
<center>
<h1>Prime Numbers in a Range</h1>
<form name="form1">
Enter Lower limit:
<input type="text" name="lower">
Enter Upper limit:
<input type="text" name="upper">
<input type="button" onclick="myFunction()" value="Try it">
<br>
</form>
<p id="demo"></p>
<script>
    function myFunction()
    {
        var s=" ",j=2,f=1;
        var i=document.form1.lower.value;
        var n=document.form1.upper.value;
        for (i;i<=n;i++)
        {
            f=1;
            for(j=2;j<i;j++)
            {
                if(i%j==0)
                {
                    f=0;
                    break;
                }
            }
        if (f==1)
            {
                s=s+"<br/>"+i;
            }
        }
        document.getElementById("demo").innerHTML=s;
    }

</script>
</center>
</body>
</html>


Output:

 


Write a JavaScript program to perform case conversion?

Code:


<!DOCTYPE html>
<html>
<body>
<p>Click the button to convert the string to uppercase letter</p>
<button onclick="myfunction()">
    Try it
</button>
<p id="demo"></p>
<script>
    function myfunction()
    {
        var str=document.form.text.value;
        var res=str.toUpperCase();
        document.getElementById("demo").innerHTML=res;
    }
</script>
<form name="form">
<input type="text" name="text" value="">
</form>
</body>
</html>


Output:


Write a JavaScript program to alert content of text box?

Code:


<!DOCTYPE html>
<html>
<body>
<center><h1>Alert the text</h1>
<form name="form1">
Enter anything:
<input type="text" name="contents">
<input type="button" onclick="myFunction()" value="Try it">   
</form>
<script>
    function myFunction ()
    {
        alert(document.form1.contents.value);
    }
</script>
</center>
</body>
</html>


Output:


Write a JavaScript program to illustrate onMouseOver event?

Code:


<!DOCTYPE html>
<html>
<body>
<p>Move the mouse over the button to display an alert box</p>
<button onmouseover="myfunction()">
    Try it
</button>
<script>
    function myfunction()
    {
        alert("This alert box will display only when the mouse move over the button");
    }
</script>

</body>
</html>


Output:


Write a JavaScript program to illustrate onKeyup event?

Code:


<!DOCTYPE html>
<html>
<body>
<p>When the user releases a key in the input field, Then function transforms the character to uppercase.</p>
Enter your name:
<input type="text" id="fname" onkeyup="myfunction()">
<script>
    function myfunction()
    {
        var x=document.getElementById("fname");
        x.value=x.value.toUpperCase();
    }
</script>
</body>
</html>


Output:


Write a JavaScript program to print strange numbers in a range?

 Code:


<!DOCTYPE html>
<html>
<body>
<center>
<h2>Strange Numbers in your Range</h2>
<p id="demo"></p>
<script>
var num,sum,temp;
var text=" ";
var i,x;
i=prompt("Enter the lower limit");
x=prompt("Enter the upper limit");
for (i; i < x; i++)
    {
        sum=0;
        num=i;
        while(num > 0)
        {
            temp=num%10;
            sum=sum+(temp*temp*temp);
            num=parseInt(num/10);

        }
        if(i==sum)
            {
                text=text + "<br/> " + i;

            }
    }
   
    document.getElementById("demo").innerHTML=text;
</script>
</center>
</body>
</html>


Output:




Write a JavaScript program to print fibonacci numbers in a range?

 Code:


<!DOCTYPE html>
<html>
<body>
<center><h1>Fibonacci Numbers in your Range</h1>
<script>
    var fib1=0,fib2=1,fib3=0;
    var nl="<br>";
    var lower=parseInt(prompt("Enter lower limit"));
    var upper=parseInt(prompt("Enter upper limit"));
    for(var i=lower;i<upper;i++)
    {
        if (fib3<=upper)
            {
                if (fib3>=lower)
                    {
                        document.write(fib3+nl);
                    }
                fib1=fib2;
                fib2=fib3;
                fib3=fib1+fib2;
            }
    }

</script>
</center>
</body>
</html>


Output: