You are on page 1of 16

Q.

1 Write a JavaScript program to validate form of vaccination or


covid test.
Program:
<html>
<body>
<script>
function validateform(){
var name=document.myform.name.value;
var password=document.myform.password.value;

if (name==null || name==""){
alert("Name can't be blank");
return false;
}else if(password.length<6){
alert("Password must be at least 6 characters long.");
return false;
}
}
</script>
<body>
<form name="myform" method="post"
action="http://www.javatpoint.com/javascriptpages/valid.jsp"
onsubmit="return validateform()" >
Name : <input type="text" name="name"><br/>
Address: <input type="varchar" name="address"><br/>
Contact: <input type="integer" name="contact"><br/>
Aadhar Card: <input type="integer" name="aadharcard"><br/>
Emailid: <input type="varchar" name="emailid"><br/>
Password: <input type="password" name="password"><br/>
<input type="submit" value="register">
</form>
</body>
</html>
Output:

Q2.Write a JavaScript program for implementation of stack using


linked list.
Program:
<html>
<body>
<script>
class node{
constructor(val){
this.val=val;
this.next=null;
}
}
class stackSinglyLinkedList{
constructor(){
this.head=null;
this.tail=null;
this.length=0;
}
push(val){
let newnode=new node(val);
if(!this.head){
this.head=newnode;
this.tail=this.head;
}else{
this.tail.next=newnode;
this.tail=newnode;
}
this.length++;
return this;
}
pop(){
if(!this.head) return undefined;
var current=this.head;
var newtail=current;
while(current.next){
newtail=current;
current=current.next;
}
this.tail=newtail;
this.tail.next=null;
this.length--;
if(this.length==0){
this.head=null;
this.tail=null;
}
return current;
}
display(){
var curr=this.head;
var str="";
while(curr){
str+=curr.val+" ";
curr=curr.next;
}
document.write("<br>"+str);
}
}
let l=new stackSinglyLinkedList();
l.display();
l.push(10);
l.push(20);
l.push(30);
l.push(40);
l.push(50);
l.display();
document.write(" :- 5 elements are pushed <br> ");
l.pop()
l.pop();
l.display();
document.write(" :- 2 elements are popped <br> ");
l.push(60);
l.push(70);
l.display();
document.write(" :- 2 elements are pushed <br> ");
l.pop();
l.display();
document.write(" :- 1 element is popped <br> ");
l.push(80);
l.push(90);
l.push(100);
l.display();
document.write(" :- 3 elements are pushed <br> ");
</script>
</body>
</html>

Output:
Q.3 Write a JavaScript program for implementation of Rain Terrraces.

Program:

<script>

function maxWater(arr, n)

let res = 0;

for(let i = 1; i < n - 1; i++)

let left = arr[i];

for(let j = 0; j < i; j++)

{
left = Math.max(left, arr[j]);

let right = arr[i];

for(let j = i + 1; j < n; j++)

right = Math.max(right, arr[j]);

res += Math.min(left, right) - arr[i];

return res;

let arr = [ 0, 1, 0, 2, 1, 0,

1, 3, 2, 1, 2, 1 ];

let n = arr.length;

document.write(maxWater(arr,n));

</script>

Output:
Q4.Write a JavaScript program for implementation of circular queue.
Program:
<script>
// Edit your script here
class CircularQueue{
constructor(size){
this.element=[];
this.size=size
this.length=0
this.front=0
this.back=-1
}
isEmpty(){
return(this.length==0)
}
enqueue(element){
if(this.length>= this.size)throw(new Error("Maximum length exceded"))
this.back++
this.element[this.back% this.size]=element
this.length++
}
dequeue(){
if(this.isEmpty()) throw(new Error("No elements in the queue"))
const value =this.getFront()
this.element[this.front % this.size]=null
this.front++
this.length--
return value
}
getFront(){
if(this.isEmpty()) throw(new Error("No elements in the queue"))
return this.element[this.front % this.size]
}
clear(){
this.element=new Array()
this.length=0
this.back=0
this.front=-1
}
disp(){
for(let i=0;i<this.element.length;i++){
document.write(this.element[i] + " ");
}
document.write("<br>");
}
}
l1=new CircularQueue(5);
l1.disp();
l1.enqueue(29);
l1.disp();
l1.enqueue(21);
l1.disp();
l1.enqueue(27);
l1.disp();
l1.enqueue(21);
l1.disp();
l1.enqueue(24);
l1.disp();
l1.dequeue();
l1.disp();
l1.enqueue(17);
l1.disp();
l1.enqueue(56);
l1.disp();
l1.enqueue();
l1.disp();

</script>
Output:
Q5.Write a JavaScript program for implementation of fast powering
and euclidean.
i) Fast Powering:
Program:
<script>

function pow_num(a,b){
if(a==0) {
retuen 0;
}
ans=1;
while(b !=0) {
if((b & 1) ! =0) {
ans *=a;
}
b >>=1;
a *=a;
}
return ans;
}
A=7; b=13;
document.write(“Power of “+a+” raise to “+b+” is :”
+pow_num(a,b))
</script>

Output:

ii) Euclidean:
Program:
<script>
function gcd( a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
let a = 10, b = 15;
document.write( "GCD(" + a + ", "
+ b + ") = " + gcd(a, b) +"<br/>");

a = 35, b = 10;
document.write( "GCD(" + a + ", "
+ b + ") = " + gcd(a, b) +"<br/>")
a = 31, b = 2;
document.write( "GCD(" + a + ", "
+ b + ") = " + gcd(a, b) +"<br/>");
</script>
Output:

You might also like