QB Answer
QB Answer
Ans :
<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'};
<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 :
<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>
<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>
<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>
<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>");
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>");
}
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(" ");
for(k=1; k<i*2; k++)
document.write(" * ");
document.write("<br><br>");
}
</script>
</body>
</html>