In the following code,
let sum = (sum, num) => sum + num;
let largest = (largest, current) => largest < current.reduce(sum, 0) ? current.reduce(sum, 0) : largest;
function largestOfFour(arr) {
return arr.reduce(largest, 0)
}
console.log(largestOfFour([[4, 5, 1, 3], [1000, 1001, 857, 1], [13, 27, 18, 26], [32, 35, 37, 39]]));
I have each array being summed in a reduce, then having those compared to the largest current sum of an array, What I'm trying to figure out is if I can carry over the evaluation used in the latter portion of the conditional operand, to the truthy operand between the "?" and ":"
current.reduce(sum, 0)
what I'm hoping for is something to the effect of the following...
let largest = (largest, current) => largest < current.reduce(sum, 0) ?: largest;
Trying to further understand the limitations and possibilities of the ternary specifically. I'm pretty sure I've used the ternary in another language where I could just leave the second operand empty and the first operand would be piped into the return value. Maybe that's not for JS, or maybe I'm limited by the fact I have a conditional being evaluated without storing the second part of it for later use.
I know this may not enhance readability and the computational cost of evaluating twice is negligible. This is just me exploring the ins and outs.
I've used MDN, w3schools and several other resources, I assume the fact I haven't seen this implemented there that it's not possible but curious if someone could point me in the right direction or show me how to make this work