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;
[Link]("Start <br/>");
while(x<20)
{
if(x==5)
{break;}
x=x+1;
[Link](x + "<br/>");
}
[Link]("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;
[Link]("Entering the loop:<br /> ");
while (x < 10)
{
x = x + 1;
if (x == 5) {
continue;
}
[Link]( x + "<br />");
}
[Link]("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= "[Link]" onclick
="javascript:[Link]()"/><br><br/>
<img src= "[Link]" onclick
="javascript:[Link]()"/><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 < [Link] ; i++)
{
if ([Link](string[i]) !== -1)
{
vowelsCount += 1;
}
}
return vowelsCount;
}
[Link](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=[Link]("here");
[Link](n);
</script>
</body>
</html>
6. Write a java script code to demonstrate function calling from another function.
Ans :
<html>
<head>
<script>
function Calling() {
[Link] ("Inside Calling Function...");
}
function Caller()
{
[Link] ("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()
{
[Link]=false;
}
function DisableFunction()
{
[Link]= 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)
[Link]("demo").innerHTML += i+" is even.<br>";
else
[Link]("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>
[Link] 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>
[Link]("Squares of Numbers : ");
for(i = 1; i <= 10; i++)
{
[Link]("<br> " + i + " = " + i*i);
}
</script>
</body>
</html>
[Link] a JavaScript program to check number is Armstrong or not.
Ans :
<html>
<head>
<script>
function Armstrong()
{
var flag,number,remainder,addition = 0;
number = Number([Link]("N").value);
flag = number;
while(number > 0)
{
remainder = number % 10;
addition = addition + remainder*remainder*remainder;
number = parseInt(number/10);
}
if(addition == flag)
{
[Link]("The number is an Armstrong number.");
}
else
{
[Link]("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>
[Link] Java script to implement Array functionalities.
Ans :
<html>
<body>
<script>
[Link]("Array Functionalities :"+ "<br/>");
var a=[20,30,10,60,40];
[Link]("1. The Length of array is : " + [Link]);
[Link]("</br>");
[Link]("2. Displaying Array Elements:");
for (i=0; i<[Link]; i++){
[Link](a[i] + "<br/>");
}
[Link]("3. The elements is Added in array:" + "<br>");
a[[Link]]=70;
a[[Link]]=90;
[Link]("Display Array Elements:");
[Link]("</br>");
for (i=0; i<[Link]; i++){
[Link](a[i] + "<br/>");
}
[Link]();
[Link]("4. Display Array Elements After Sorting:");
[Link]("</br>");
for (i=0; i<[Link]; i++){
[Link](a[i] + "<br/>");
}
var num=[Link]();
[Link]("Removed element is : " + num + "<br>");
[Link]("5. Display Array element After shift function is :
" + "<br>");
for (i=0;i<[Link];i++){
[Link](a[i] + "<br>");
}
var length = [Link](100);
[Link]("6. Display Array element After push function is: "
+ a );
[Link]("</br>");
var element = [Link]();
[Link]("7. Display Array element After pop function is: " +
a );
[Link]("</br>");
var arr = [Link]();
[Link]("8. Display Array element After Reverse function is:
" + arr );
</script>
</body>
</html>
[Link] 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()
{
[Link]("User ID : "+ [Link] + "<br/>" + "Password :
"+[Link]);
}
</script>
</body>
</html>
[Link] a program to display prime numbers using function.
Ans :
<html>
<head>
<script>
function prime(){
var num, i,check=0;
num=Number([Link]("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>
[Link] 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++)
[Link](" ");
for(k=1; k<i*2; k++)
[Link](" * ");
[Link]("<br><br>");
}
</script>
</body>
</html>