wt questions on programs
wt questions on programs
2. Write a program and execute in JavaScript to compute the real roots of quadratic
equation, asking for coefficients of equation (a,b,c) from the user. [Use
prompt,Math.sqrt]
3. Write a program in javaScript to display the following table: [Spring,2012]
1
Web Technology Practical
Compiled by: Er. Ramu Pandey
Asst. Prof. , NCIT
(Use document.write(“<table border=’1’><tr><th>S.No.</th></tr> ……”);)
4. Write a program in JavaScript that takes username as input from Prompt box and
display that name as an output in Alert box. [Spring,2011]
5. Write a program in JavaScript to display the multiplication table of a given number.
[Fall,2012]
Solution:
<script type='text/javascript'>
var num = prompt("Enter Number", "0") //prompt user to enter the number
var num = parseInt(num); //parse the num to number
var i = 0;
document.write('<table border="1" cellspacing="0">');
for(i=1;i<10;i++) {
document.write("<tr><td>" + num + " x " + i + " = " + num*i +
"</td></tr>");
}
document.write("</table>");
</script>
6. Write a JavaScript to find the reverse of a string supplied using prompt. [spring,2014]
Solution:
Function reverseString(str){
Var array=str.split(“”);
array.reverse();
var result=array.join(“”);
return result;
}
Console.log(reverseString(“hello”));
7. Write a program in JavaScript to display a prompt for 2 numbers and show its sum in
alert box. [Spring,2015]
8. Write a program in JavaScript to alter the visibility of an image of HTML document.
[Spring,2016]
9. Write a program in JavaScript to change the color of text displayed in HTML
document to red.
10. Write a JavaScript program to create a digital clock. The clock is displayed in webpage
and displays the current time. The clock displays new time in the interval of 1 second.
[Spring,2011]
Solution:
<!DOCTYPE html>
<html>
<head>
<script type=”text/javascript”>
function startTime() {
var today = new Date();
2
Web Technology Practical
Compiled by: Er. Ramu Pandey
Asst. Prof. , NCIT
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('txt').innerHTML =
h + ":" + m + ":" + s;
var t = setTimeout(startTime, 1000); //1s=1000ms
}
function checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
</script>
</head>
<body onload="startTime()">
<div id="txt"></div>
</body>
</html>
11. Write a JavaScript program to validate the form input fields name, email and contact
number and alert empty error message if the field is empty.
12. Write a JavaScript program to validate the form input fields name, email and contact
number using pattern matching and alert error message if the field does not match the
pattern.
Solution:
For name (Method 1):
var regex = /^[a-zA-Z ]{2,30}$/;
var ctrl = document.getElementById(id);
if (regex.test(ctrl.value)) {
return true;
}
For name (Method 2):
var regex = /^[a-zA-Z ]{2,30}$/;
var ctrl = document.getElementById(id).value;
if(ctrl.match(regex)){
return true;
}
For name (Method 3):
var regex = /^[a-zA-Z ]{2,30}$/;
var ctrl = document.getElementById(id).value;
if(ctrl.search(regex) >=0){
return true;
}
3
Web Technology Practical
Compiled by: Er. Ramu Pandey
Asst. Prof. , NCIT
For email:
var pattern = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
For phone number:
var phoneno = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
or
var phone= /^(\([0-9]{3}\)\s*|[0-9]{3}\-)[0-9]{3}-[0-9]{4}$/
(validates '(078)789-8908';
'(078) 789-8908'; // Note the space )
13. Create a page with button and textbox. The textbox should display a value ‘0’ initially.
Write a JavaScript function so that when you click the button the value of textbox
increases with 1 per click. [Spring,2013]
14. WAP in JavaScript to find the factorial of entered number.
<!doctype html>
<html>
<head>
<script>
function show(){
var i, no, fact;
fact=1;
no=Number(document.getElementById("num").value);
for(i=1; i<=no; i++)
{
fact= fact*i;
}
document.getElementById("answer").value= fact;
}
</script>
</head>
<body>
Enter Num: <input type="text" id="num">
<button onclick="show()">Factorial</button>
<input type="text" id="answer">
</body>
</html>
4
Web Technology Practical
Compiled by: Er. Ramu Pandey
Asst. Prof. , NCIT
15. Using JavaScript and forms prepare a HTML document to find (and display in textbox)
the factorial of a entered number. Also display the message in alert box if the factorial
is divisible both by 2 and 5.
16. Write JavaScript programs for:
- Moving Element & Element Visibility
- Changing Colors & Fonts
- Dynamic Content & Stacking Elements
- Dragging and Dropping Elements
- Event Handling