-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc1_initial_solution.js
78 lines (55 loc) · 1.92 KB
/
gc1_initial_solution.js
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
74
75
76
77
78
// I took the pseudocode given to me and here is what I came up with:
// Pseudo code for finding sum of a list.
//Initialize constant variable SumList = 0.
//Make a loop which iterate over all the elements in that list and
//sum the varibales and store it inside SumList as sumList = sumList + //var.
//print sumList on console using console.log(sumList)
// Ensuring to make we start the loop, I used 'for' to define the variable
var sumList = 0;
var randomList = [1, 2, 3, 4, 5, 6, 7 ,8, 9, 10];
for(var i = 0; i < randomList.length; i++)
{
sumList += randomList[i];
}
console.log(sumList);
//Pseudo code for finding Median
//initialize constant variable median=0.
//Sort the list as list.sort()
//Find the length of list as listlength=list.length
//if listlength is odd{
// find the middle number middle as Math.round(listlength-1)
// Now median=list[middle-1]
//}
//else{
// middle=list.length/2
// median=(list[middle]+list[middle-1])/2
//}
//print median on console using console.log(median)
var median = 0;
var middle;
var list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var listlength = list.length;
list.sort();
if (listlength%2 !== 0){
middle = Math.round(listlength-1);
median = list[middle-1];
}
else{
middle = listlength/2;
median = (list[middle]+list[middle-1])/2;
}
console.log(median);
// Pseudo code for finding mean of a list.
//initialize constant variable mean=0.
//find the sum of list using the same method as in idea 1.
//find the length of the list as listlength=list.length
//find the mean as mean=sumList/listlength
//print mean on console using console.log(mean)
var mean = 0;
for(var i = 0; i < randomList.length; i++)
var list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var listlength = list.length;
{
mean = sumList/list.length;
}
console.log(mean);