JavaScript Basic Programs

Home  >>  JavaScript Tutorials  >>  JavaScript Basic Programs

JavaScript Basic Programs

23
Sep,2017

0

Basic Programs of JavaScript

——————————————-Javascript Tutorials———————————————-

<script type=”text/javascript”>
function show()
{
return “xyz”;
}
var names=[“shagun”,”richa”,show,[“shgn”,”sharma”]];
document.write(names[2]+”<br>”);
document.write(names[2]()+”<br>”);
document.write(names[3]+”<br>”);
document.write(names[3][0]+”<br>”);
</script>
——————————————————————————————————————–
<script type=”text/javascript”>
var a=”Shgn”
function show()
{
var a=”Sharma”
document.write(a + “<br>”)
}
document.write(a + “<br>”)
show()

</script>
——————————————————————————————————————–
<script type=”text/javascript”>
var a=”Shgn”
function show()
{
a=”Sharma”
document.write(a + “<br>”)
}
document.write(a + “<br>”)
show()
document.write(a)

</script>

——————————————————————————————————————–
//Shift(remove element in begining), Unshift(Add element in begining), Push(add element at end) and Pop(delete element in the end) functions

<script type=”text/javascript”>
var a=”Shgn”
function show()
{
var names=[“a”,”b”,”c”,”d”]
document.write(names)
document.write(“<br>”+ names.shift() + ” is deleted from the array”)
document.write(“<br>”+ names.shift() + ” is deleted from the array”)
document.write(“<br>” + names.pop() + ” is deleted from end”)
document.write(“<br>” + names.pop() + ” is deleted from end”)
document.write(“<br>”+ names.shift() + ” is deleted from the array”)

}

show()
</script>

<!————————————————Array——————————————————->
<html>
<head>
<title>Array Practice</title>
</head>
<body>
<script>
var temp;
var n=prompt(“Enter the number of arrays: “);
var a=new Array();
for(i=0;i<n;i++)
{
a[i]=prompt(“Enter “+(i+1)+” value: “);
}

document.write(“Enter number “+a);

function sort()
{
for(i=0;i<=n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;

}
}
}
document.write(“<br>”+a);
}

function search()
{
var q=parseInt(prompt(“Enter number to search: “));
for(i=0;i<=n;i++)
{
if(a[i]==q)
{
document.write(“<br>element is at “+(i+1)+”<br>”);
}

}
}
sort();
search();
</script>
</body>
</html>

<!—————————Operations (Push, Pop, Shift, Unshift)——————————–>
<html>
<head>
<title>Javascript Tutorials</title>
</head>
<body>
<script type=”text/javascript”>
function show2()
{
var n=prompt(“Enter the number of elements: “);
var a=new Array();
for(var i=0;i<n;i++)
{
a[i]=prompt(“Enter “+ (i+1) +” value: “);
document.write(“<br>”);
}
//a=a.join(“-“);
document.write(“Elements in array: “+a+”<br>”);
var end=0;
while(end==0)
{
document.write(“<br>Enter your Choice: <br>”);
document.write(“1. Shift<br>2. Unshift<br>3. Push<br>4. Pop<br>5. End Process<br>”);
var z=parseInt(prompt(“Your Choice”));
<!–document.write(z);–>
if(z==1)
{
var s=a.shift();
document.write(“Shift Element “+s+”<br>”);
//document.write(z);

}

else if(z==2)
{
var m=parseInt(prompt(“Enter the element inserted at start “));
a.unshift(m);
document.write(“After Unshift operation “+a);
}

else if(z==3)
{
a.push();
document.write(“After Push operattoin we have”+a+”<br>”);
}

else if(z==4)
{
a.pop();
document.write(“After Pop operattoin we have”+a+”<br>”);
}
else if(z==5)
{
end=1;
}
else
{
alert(“Not in Option..! Choose other”);
}
}
}
show2();
</script>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *