forked from sf-wdi-gaia/js-data-types
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindependent-practice.js
More file actions
77 lines (53 loc) · 1.58 KB
/
independent-practice.js
File metadata and controls
77 lines (53 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* Arrays
* Most of your answers should be stored in variables called q1, q2 etc.. and the variables printed to the console.
(i.e) console.log("Question 1" + q1)
*/
/**
* Question 1
* Create an array of image sources. Use image1.png, image2.png, and image3.png as the array values.
*/
// Your code here
var q1 = ["image1.png","image2.png","image3.png"];
/**
* Question 2
* Using the array from Question 1, store the first element of the array in variable q2.
*/
// Your code here
var q2 = q1[0];
/**
* Question 3
* Get the length of the first array (number of elements in the array) and store it in variable q3
*/
// Your code here
var q3 = q1.length;
/**
* Question 4
* Using the array from Question 1, store the last element of the array in variable q4. Hint: How can we get the number of elements in the array?
*/
// Your code here
var q4 = q1[q1.length-1];
// ____________________________________________________________________________
/**
* Arrays + Iteration
*/
/**
* Question 5
* Create an array of numbers using 1,2,3, and 4 as values. Use forEach to increase each value by 1 and store the value back in the array.
The end result should be an array of numbers with values 2,3,4,5
*/
// Your code here
var q5 = [1,2,3,4,5];
q5.forEach(function(el,index){
q5[index]=el+1;
});
/**
* Question 6
* Using the array from Question 5, find the average of the numbers in the array (average=sum of all numbers/number of numbers). Store the average in q6.
*/
// Your code here
var sum = 0;
for(i = 0 ; i < q5.length; i++ ){
sum += q5[i];
}
var q6 = sum / q5.length;