0% found this document useful (0 votes)
28 views10 pages

QB Answer

Uploaded by

yashsadafal502
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views10 pages

QB Answer

Uploaded by

yashsadafal502
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1. Describe the use of break and continue statement with example.

Ans :

1. Break Statement : The break statement, which was briefly introduced


with the switch statement, is used to exit a loop early, breaking out of the
enclosing curlybraces.
<html>
<body>
<script>
var x=0;
document.write("Start <br/>");
while(x<20)
{
if(x==5)
{break;}
x=x+1;
document.write(x + "<br/>");
}
document.write("Exit");
</script>
</body>
</html>
2. Continue Statement:
• The continue statement tells the interpreter to immediately start the
nextiteration of the loop and skip the remaining code block.

<html>
<body>
<script>
var x = 1;
document.write("Entering the loop:<br /> ");
while (x < 10)
{
x = x + 1;
if (x == 5) {
continue;
}
document.write( x + "<br />");
}
document.write("Exiting the loop!<br /> ");
</script>
</body>
</html>
2. Explain the concept of associative array in Javascript.
Ans :
• An associative array is simply a set of key value pairs.
• The value is stored in association with its key and if you provide the key the
array will return the value.
• This is all an associative array is and the name comes from the association
between the key and the value.
• The key is a sort of generalized address that can be used to retrieve the stored
value.
• For example: array = {key1: 'value1',key2:'value2'};

3. Define intrinsic functions. Give suitable example.


Ans :
• Intrinsic function means the built-in functions that are provided by JavaScript.
• The JavaScript provides the Intrinsic function for Submit & Reset Button. It
can beused while submitting the form or resetting the form fields.
• The submit() method of the form object can be used to send the form
to theserver in exactly same way as if the user has pressed the submit
button.

<html>
<body>
<form name="myform">
Roll Number : <input type= "text" name="roll"/><br><br/>Name :
<input type= "text" name="name"/><br><br/>
<img src= "Submit.png" onclick
="javascript:document.forms.myforms.submit()"/><br><br/>
<img src= "Reset.png" onclick
="javascript:document.forms.myforms.submit()"/><br><br/>
</form>
</body>
</html>
4. Write a JavaScript function to count the number of vowels in a given string.
Ans :

<html>
<body>
<script>
function noOfVowels(string) {
var listOfVowels = 'aeiouAEIOU';
var vowelsCount = 0;
for(var i = 0; i < string.length ; i++)
{
if (listOfVowels.indexOf(string[i]) !== -1)
{
vowelsCount += 1;
}
}
return vowelsCount;
}
document.write(noOfVowels("I can do this."));
</script>
</body>
</html>
5. Define string? List any four methods of it.
Ans :

• String is a collection of characters.


• Commonly used method of string objects is concatenating two strings, converting
the string to upper case or lower case, finding the substring of a given string.
• String written within the single or double quotes.
Manipulating a String Methods :
1. charAt()
2. charCodeAt()
3. concat()
4. indexOf()
5. lastIndexOf()
<html>
<body>
<script>
var s1="javascript here";
var n=s1.indexOf("here");
document.write(n);
</script>
</body>
</html>
6. Write a java script code to demonstrate function calling from another function.
Ans :

<html>
<head>
<script>
function Calling() {
document.write ("Inside Calling Function...");
}
function Caller()
{
document.write ("Inside Caller Function..."+ "</br>"); Calling();
}
</script>
</head>
<body onload = Caller();>
</body>
</html>
7. How to disable particular element on the form give example.
Ans :
• We can restrict some fields on the form by using disabled.
• If disabled property of particular form element is set to true then user cannot edit that
element. Similarly on setting property to false we can edit the field.

<html>
<head>
<script>
function EnableFunction()
{
documents.forms.myform.name.disabled=false;
}
function DisableFunction()
{
documents.forms.myform.name.disabled= true;
}
</script>
<form name="myform">
User Name : <input type= "text" name="name"/><br><br/>
<input type= "button" value="Disable Name Field"
onclick="DisableFunction()"/><br><br/>
<input type= "button" value="Enable Name Field"
onclick="EnableFunction()"/><br><br/>
</form>
</body>
</html>
8. How will you create password field in a HTML form?
Ans :
The password field is typically created on the form.
Following code can be written to create it :

<form name=”form1”>
Password : <input type = “password”/>

</form>

9. Write a program to display even and odd numbers using function.


Ans :

<html>
<head>
<title>Evenodd</title>
</head>
<script>
function OddEven()
{
var i;
for(i=0;i<=15;i++)
{
if(i%2==0)
document.getElementById("demo").innerHTML += i+" is even.<br>";
else
document.getElementById("demo").innerHTML += i+" is odd.<br>";
}
}
</script>
<body>
<h3>Find odd or even.</h3>
<button onclick="OddEven()">Check Odd Even</button>
<p id="demo"></p>
</body>
</html>

10.Write a JavaScript to display squares of 1 to 10 numbers using for loop.


Ans :
<html>
<head>
<title>Square of numbers from 1 to 10</title>
</head>
<body>
<script>
document.write("Squares of Numbers : ");
for(i = 1; i <= 10; i++)
{
document.write("<br> " + i + " = " + i*i);
}
</script>
</body>
</html>

11.Write a JavaScript program to check number is Armstrong or not.


Ans :

<html>
<head>
<script>
function Armstrong()
{
var flag,number,remainder,addition = 0;
number = Number(document.getElementById("N").value);

flag = number;
while(number > 0)
{
remainder = number % 10;
addition = addition + remainder*remainder*remainder;
number = parseInt(number/10);
}

if(addition == flag)
{
window.alert("The number is an Armstrong number.");
}
else
{
window.alert("The number is not an Armstrong number.");
}
}
</script>
</head>
<body>
<br>
Enter The Number : <input type="text" name="n" id = "N"/>
<br>
<br>
<button onClick="Armstrong()">Check here.</button>
</body>
</html>

12.Develop Java script to implement Array functionalities.


Ans :

<html>
<body>
<script>
document.write("Array Functionalities :"+ "<br/>");
var a=[20,30,10,60,40];
document.write("1. The Length of array is : " + a.length);
document.write("</br>");

document.write("2. Displaying Array Elements:");


for (i=0; i<a.length; i++){
document.write(a[i] + "<br/>");
}

document.write("3. The elements is Added in array:" + "<br>");


a[a.length]=70;
a[a.length]=90;
document.write("Display Array Elements:");
document.write("</br>");
for (i=0; i<a.length; i++){
document.write(a[i] + "<br/>");
}

a.sort();
document.write("4. Display Array Elements After Sorting:");
document.write("</br>");
for (i=0; i<a.length; i++){
document.write(a[i] + "<br/>");
}

var num=a.shift();
document.write("Removed element is : " + num + "<br>");
document.write("5. Display Array element After shift function is :
" + "<br>");
for (i=0;i<a.length;i++){
document.write(a[i] + "<br>");
}

var length = a.push(100);


document.write("6. Display Array element After push function is: "
+ a );
document.write("</br>");

var element = a.pop();


document.write("7. Display Array element After pop function is: " +
a );
document.write("</br>");

var arr = a.reverse();


document.write("8. Display Array element After Reverse function is:
" + arr );
</script>
</body>
</html>

13.Write a Java Script to design a form to accept values for User ID and password.
Ans :

<html>
<body>
<form name="login">
Enter Username : <input type="text" name="userid"><br><br>
Enter Password : <input type="password" name="pswrd"><br><br>
<input type="button" onclick="display()" value="Display">
</form>
<script language="javascript">
function display()
{
document.write("User ID : "+ login.userid.value + "<br/>" + "Password :
"+login.pswrd.value);
}
</script>
</body>
</html>
14.Write a program to display prime numbers using function.
Ans :

<html>
<head>
<script>
function prime(){
var num, i,check=0;
num=Number(document.getElementById("num_input").value);
for(i=2; i<num; i++)
{
if(num%2==0)
{
check++;
break;
}
}
if(check==0){
alert("Prime Number");
}
else
{
alert("Not a Prime Number");
}
}
</script>
</head>
<body>
Enter Any Number : <input id="num_input"><br/>
<br/>
<button onclick="prime()">Submit</button>
</body>
</html>
15.Write a JavaScript program to construct the following pattern.
Ans :

<html>
<body>
<script>
var i, j, k;
for(i=1; i<=4; i++)
{
for(j=i; j<4; j++)
document.write("&nbsp;&nbsp;");
for(k=1; k<i*2; k++)
document.write(" * ");
document.write("<br><br>");
}
</script>
</body>
</html>

You might also like