Saturday, January 7, 2012

Javascript exercise: push/pop of array

  • push():
    adds new elements to the end of an array, and returns the new length.
  • pop():
    removes the last element of an array, and returns that element.


Example:
Javascript exercise: push/pop of array
<!doctype html>
<head>
<title>Mobile-Web-App: push/pop of array in Javascript</title>
<meta charset="utf-8">

<script>
function myOnLoad(){

var arrayinfo;

var array = new Array();
array.push("Sunday");
array.push("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

arrayinfo = "length of array: " + array.length + "<" + "BR/" + ">";

while(array.length>0){
arrayinfo += array.pop() + "<" + "BR/" + ">";
}

elementInfo = document.getElementById("info");
elementInfo.innerHTML = arrayinfo;

}

window.onload = myOnLoad;

</script>


</head>
<body>
<h1>Javascript exercise: push/pop of array</h1>

<p id="info"></p>

</body>
</html>

No comments:

Post a Comment