Consider the input array: [2, 1, 4, 4, 3]
In this array, a total of n-1 patterns can be identified from left to right.
The resulting output will be the number 7
since the arrays are grouped as follows:
[2] [1] [4] [4] [3]
- 1 group (n)
[4, 3]
- 1 group (n-1)
[2, 1]
- 1 group (n-1)
Output: 7
(arrays)
This snippet provides an initial attempt where everything is simply summed up together.
let numbers = [2, 1, 4, 4, 3];
let sum = numbers.reduce(function (previousValue, currentValue) {
return previousValue + currentValue;
});
console.log(sum);
I would appreciate guidance on how to properly solve this problem in JavaScript. Thanks!