Page 382 - Web_Application_v2.0_C12_Fb
P. 382
var fruits = ["Apple","Banana","Orange","Grapes","Pear"];
fruits[5]="Dragonfruit";
document.write("The contents of the array are...."+"<BR>");
for (var i = 0; i < fruits.length; i++) {
document.write(fruits[i]+"<br>");
}
</SCRIPT>
</BODY>
</HTML>
Output of the preceding code is as follows:
14. Create a webpage with JavaScript code that displays the sum of 5 elements in an array using a while
loop.
Ans. Sum of Numbers.html code:
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>SUM OF NUMBERS</TITLE>
<SCRIPT>
function calculateSum() {
var numbers = [5, 10, 15, 20, 25];
var sum = 0;
var i = 0;
while (i < numbers.length) {
sum += numbers[i];
i++;
}
380 Touchpad Web Applications (Ver. 2.0)-XII

