-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.js
98 lines (78 loc) · 2.13 KB
/
array.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//problem set for: Reverse array, Uniform array, sum of array
//And max values in array
printReverse([1, 3, 4, 6, 8, 10])
printReverse(["a", "b", "c", "d"])
//prints the reverse of an array
function printReverse(array) {
for (var i = array.length - 1; i >= 0; i--) {
console.log(array[i]);
}
}
console.log(isUniform([1, 1]))
console.log(isUniform([2, 1, 1, 1, 1]))
console.log(isUniform(["a", "a", "b"]))
console.log(isUniform(["a", "b", "c"]))
console.log(isUniform(["a", "b", "c"]))
//identifies if the array is all uniform
function isUniform(array) {
for (var i = 0; i < array.length; i++) {
if (i + 1 < array.length) {
if (array[i] != array[i + 1]) {
return false;
}
}
}
return true;
}
console.log(sumArray([]))
console.log(sumArray([1, 2, 3, 4, 5])) //sum: 15
console.log(sumArray([-10, 1, 2, -20])) //sum: -27
console.log(sumArray([1.5, 1])) //sum: 2.5
function sumArray(array) {
var sum = 0;
if (array.length === 0)
return sum;
for (var i = 0; i < array.length; i++) {
sum = sum + array[i];
}
return sum;
}
console.log("Max: " + max(["a","b","c"]))
console.log("Max: " + max([]))
console.log("Max: " + max([1,2,3,5])) //max: 5
console.log("Max: " + max([5,4,3,2,1])) //max: 5
console.log("Max: " + max([34,5,789,6.7])) //max: 789
console.log("Max: " + max([34,-5,789,-6.7])) //789
console.log("Max: " + max([-10, -5, -9, -15, 0])) //0
function max(array){
var areWeMax;
//could also sort the values
//could also check the type in the array
if (array.length === 0){
//gentle on the user
console.log("Empty array: nothing to sum.")
return;
}
areWeMax = array[0];
for(var i = 0; i < array.length; i++){
if(isNaN(array[i])){
console.log("Can't sum max of non numbers");
return;
}
if(areWeMax < array[i]){
areWeMax = array[i];
}
}
return areWeMax;
}
console.log(maxSort([1,2,3,4,5]))
console.log(maxSort([1,3,4,-3,5]))
function compareNumbers(a, b)
{
return a - b;
}
function maxSort(array){
var areWeMax = array.sort();
console.log("Max" + areWeMax);
return areWeMax[array.length-1];
}