<! searching an array-->
<html>
<head>
<title>searching a element</title>
</head>
<body>
<script language="JavaScript">
var a=["Ram","kiran",34,36.54,"sita"];
// a=["Ram","kiran",34,36.54,'sita',"sita"];//valid
//display the array
document.writeln("<p>");
var n=a.length;
for(var i=0;i<n;i++)
{
document.write(a[i]+ " ,");
}
document.writeln("</p>");
//ask the user to remove item
var r=prompt("Please type what you want to remove"," ");
var temp=new Array(a.length-1);//we have to remove one element so -1 is must else we get "undefined"
var m=0;
function searchItem()
{
//for loop searches, and removes
for(var i=0;i<n;i++)
{
if(a[i]==r)
{
//does nothing
;
}
else
{
temp[m]=a[i];
m++;
}
}//end of for loop
//copying from array temp to array a
a=temp;
}//end of function
searchItem();
//display the new array
document.write("<p>");
n=a.length;
for(var i=0;i<n;i++)
{
document.write(a[i]+" ,");
}
document.writeln("</p>");
// document.close();
</script>
</body>
</html>
<html>
<head>
<title>searching a element</title>
</head>
<body>
<script language="JavaScript">
var a=["Ram","kiran",34,36.54,"sita"];
// a=["Ram","kiran",34,36.54,'sita',"sita"];//valid
//display the array
document.writeln("<p>");
var n=a.length;
for(var i=0;i<n;i++)
{
document.write(a[i]+ " ,");
}
document.writeln("</p>");
//ask the user to remove item
var r=prompt("Please type what you want to remove"," ");
var temp=new Array(a.length-1);//we have to remove one element so -1 is must else we get "undefined"
var m=0;
function searchItem()
{
//for loop searches, and removes
for(var i=0;i<n;i++)
{
if(a[i]==r)
{
//does nothing
;
}
else
{
temp[m]=a[i];
m++;
}
}//end of for loop
//copying from array temp to array a
a=temp;
}//end of function
searchItem();
//display the new array
document.write("<p>");
n=a.length;
for(var i=0;i<n;i++)
{
document.write(a[i]+" ,");
}
document.writeln("</p>");
// document.close();
</script>
</body>
</html>
No comments:
Post a Comment