-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumSomeArrayValues.js
More file actions
151 lines (104 loc) · 3.77 KB
/
sumSomeArrayValues.js
File metadata and controls
151 lines (104 loc) · 3.77 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//sort((a,b) => a-b)[0]
function sumArray(array) {
if (!array || array.length <= 2) return 0;
// Sort the array from smallest to largest
let sorted = array.sort((a, b) => a - b);
// Slice off the first and last (lowest and highest)
let trimmed = sorted.slice(1, -1);
// Sum the remaining numbers
let sum = 0;
for (let i = 0; i < trimmed.length; i++) {
sum += trimmed[i];
}
return sum;
}
console.log(sumArray([6, 2, 1, 8, 10])); // 16
console.log(sumArray([1, 1, 11, 2, 3])); // 6
//BETTER SOLUTION USING REDUCE INSTEAD OF A FOR LOOP
function sumArray(array) {
if (!array || array.length <= 2) return 0;
// Sort the array from smallest to largest
let sorted = array.sort((a, b) => a - b);
// Slice off the first and last (lowest and highest)
let trimmed = sorted.slice(1, -1);
// Sum the remaining numbers using reduce
let sum = trimmed.reduce((acc, num) => acc + num, 0);
return sum;
}
/*
Here's what each part means:
!array
This checks if array is falsy.
If array is null, undefined, or something like that, !array will be true.
So this protects your code from crashing if no array was passed in.
array.length <= 2
This checks if the array has 2 or fewer elements.
If there's only 1 or 2 items, you can't remove both the highest and lowest values and still have anything left to sum — so it just returns 0.
Why both checks?
Imagine someone passes in:
null → !array catches that
[] (empty array) → array.length <= 2 catches that
[3] or [3, 5] → still too short to remove min/max
*/
//OTHER SOLUTION
function sumArray(array) {
// Handle edge cases: null, undefined, or not enough elements
if (!array || array.length <= 2) return 0;
// Initialize variables
let min = Math.min(...array);
let max = Math.max(...array);
let sum = 0;
let minRemoved = false;
let maxRemoved = false;
for (let i = 0; i < array.length; i++) {
if (array[i] === min && !minRemoved) {
minRemoved = true; // Remove only one min
continue;
}
if (array[i] === max && !maxRemoved) {
maxRemoved = true; // Remove only one max
continue;
}
sum += array[i];
}
return sum;
}
/*
What is minRemoved and maxRemoved doing?
They're flags (boolean values) used to make sure only one instance of the smallest and largest numbers gets removed.
Here's the idea:
Imagine multiple elements in the array equal the min or max.
You only want to remove one min and one max, not all of them.
So:
if (array[i] === min && !minRemoved) {
minRemoved = true;
continue;
}
This says:
"If the current number is the minimum, and I haven't removed a min yet, skip this number, and mark it as removed."
Same logic for maxRemoved.
What does continue do?
The continue statement skips the rest of the loop for that iteration.
So if you hit a min or max that hasn't been removed yet, you:
Flag it as removed.
Skip adding it to the sum.
Summary:
-minRemoved and maxRemoved are used to only skip one occurrence of the smallest and largest values.
-continue tells the loop to skip over those values when found.
So we set minRemoved = true to mark it as removed, and continue to skip adding it to the sum.
Example:
Let’s say:
js
Copy
Edit
let array = [1, 1, 2, 3, 4];
And the min is 1. You only want to remove one of those 1s, not both.
Here’s what happens in the loop:
i array[i] minRemoved Action sum
0 1 false it's min, minRemoved → true skip
1 1 true already removed min → add to sum 1
2 2 true not min or max → add to sum 3
3 3 true not min or max → add to sum 6
4 4 true not min or max → add to sum 10
Final sum = 10
*/