In: Computer Science
Write JavaScript statements to accomplish each of the following tasks:
a) Display the value of the seventh element of array f.
b) Initialize each of the five elements of one-dimensional array g to 8.
c) Total the elements of array c, which contains 100 numeric elements.
d) Copy 11-element array a into the first portion of array b, which contains 34 elements.
e) Determine and print the smallest and largest values contained in 99-element floating-
point array w.
PLEASE WRITE ALL HTML PROGRAM IN A SINGLE PROGRAM. ALL ANSWERS SHOULD BE SOLVED IN A SINGLE OUTPUT.
Code in HTML+Javascript in single page
<!DOCTYPE html>
<html>
<title>Web Page Design</title>
<head>
<script>
function test() {
//question a)
//declare an array with 10 numbers
var nums = [1,2,3,4,5,6,7,8,9,10];
//print the seventh element of array which is at index 6
document.write("a) Printing 7th element of array -> ", nums, " = ");
document.write(nums[6]);
document.write("<br/>");
//question b)
//Initialize each five elements of an array g to 8
var g = Array(5).fill(8);
document.write("b) Array b after initializing -> ", g);
document.write("<br/>");
//question c)
//Total the elements of array c which contains 100 numeric elements
//contruct an array with 100 elements
var c = [];
arraySize=100;
while(arraySize--) c.push(arraySize);
var sum=0;
for (var i = 0; i < 100; i++) sum+=c[i];
document.write("c) Sum of 100 elements = ", sum);
document.write("<br/>");
//question d)
//Copy 11-element array a into the first portion of array b, which contains 34 elements.
//create b with 34 elements
var b = [];
arraySize=34;
while(arraySize--) b.push(arraySize*2);
//create a with 11 elements
var a = [];
arraySize=11;
while(arraySize--) a.push(arraySize*3);
document.write("d) Copying a -> ", a, "<br/>to b -> ", b);
document.write("<br/>");
for (var i = 0; i < 11; i++) b[i]=a[i];
document.write("Final Array b -> ", b);
document.write("<br/>");
//copy a in b
//question e)
//Determine and print the smallest and largest values contained in 99-element floating-point array w
//contruct an array with 99 elements
var w = [];
arraySize=99;
while(arraySize--) w.push(arraySize+arraySize/100);
var small=w[0];
var large=w[0]
for (var i =1; i < 99; i++) {
if(w[i]>large) large=w[i];
if(w[i]<small) small=w[i];
}
document.write("e) Smallest number = ", small);
document.write(", Largest number = ", large);
document.write("<br/>");
}
test()
</script>
</head>
<body>
</body>
</html>
Sample document output
a) Printing 7th element of array -> 1,2,3,4,5,6,7,8,9,10 = 7
b) Array b after initializing -> 8,8,8,8,8
c) Sum of 100 elements = 4950
d) Copying a -> 30,27,24,21,18,15,12,9,6,3,0
to b -> 66,64,62,60,58,56,54,52,50,48,46,44,42,40,38,36,34,32,30,28,26,24,22,20,18,16,14,12,10,8,6,4,2,0
Final Array b -> 30,27,24,21,18,15,12,9,6,3,0,44,42,40,38,36,34,32,30,28,26,24,22,20,18,16,14,12,10,8,6,4,2,0
e) Smallest number = 0, Largest number = 98.98
Let me know in comments if you have any doubts. Do leave a thumbs up if this was helpful.