Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Tuesday 14 February 2012

Write a JavaScript that finds out multiples of 10 in 0 to 10000. On the click of button start the timer and stop the counter after 10 seconds. Display on the screen how many multiples of 10 are found out within stipulated time.


<html>
<head>
<script lang="javascript">
var arr=new Array();

var no=1,counter=0,sec=0;
function calc1()
{
if(no%10 == 0 && no<=1000000)
{
arr[counter]=no;
counter++;
}
no+=1;
sec++;
t=setTimeout("calc1()",1000);
if(sec==10)
{
f1.t1.value=counter + " Number Found";
}
}
function calc()
{
var no=0,counter=0;
var diff=0;
d=new Date();
startsec=d.getSeconds();
currentsec=startsec;
while( diff < 1)
{
if(no%10 == 0 && no<=1000000)
{
arr[counter]=no;
counter++;
}
no++;
//**********calculation of time*********
f1.t1.value=counter + " Number Found";
d=new Date();
currentsec=d.getSeconds();
if(currentsec<startsec)
currentsec=60+currentsec;
diff=currentsec-startsec;
//********************
}
}
function starttimer()
{
d=new Date();
f1.sec.value=d.getSeconds();
t=setTimeout("starttimer()",1000);
}
function stoptimer()
{
clearTimeout(t);
display();
}
function display()
{
var str="";
for(var i in arr)
{
str=str+arr[i]+" | ";
}
document.getElementById("msg").innerHTML=str;
}
</script>
</head>

<body onload="starttimer()">
<h3>PROGRAM 4 : </h3><h4>FINDING MULTIPLES OF 10</h4><BR>
<form name="f1">
<input type="text" id="t1" name="t1"/>
<input type="text" id="sec" name="sec"/>
<input type="button" name="b1" value="START (According to Time)" onclick="calc()"/>
<input type="button" name="b1" value="START (According to Timeout)" onclick="calc1()"/>
<input type="button" name="b2" value="SHOW NUMBERS" onclick="stoptimer()"/>
<div id="msg"></div>
</form>
</body>
</html>

No comments:

Post a Comment