My solution to LeetCode 42. Trapping Rain Water in JavaScript.
/**
* @param {number[]} height
* @return {number}
*/
var trap = function(height) {
let maxLeft = 0, maxRight = 0;
let left = 0;
let right = height.length -1;
let total = 0;
while (left < right) {
if (height[left] < height[right]) {
if (height[left]>maxLeft) {
maxLeft = height[left];
} else {
total += maxLeft-height[left]
}
left++;
} else {
if (height[right]>maxRight) {
maxRight = height[right];
} else {
total += maxRight-height[right]
}
right--;
}
}
return total;
};