JavaScript Challenge - Capturing Rainwater

This community-built FAQ covers the “Capturing Rainwater” code challenge in JavaScript. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the JavaScript challenge Capturing Rainwater

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

function capturingRainwater(heights) { return heights.map((n, i) => Math.max(0, Math.min( Math.max(...heights.slice(i + 1)), Math.max(...heights.slice(0, i)) ) - n) ).reduce((a, b) => a + b, 0); } const testArray = [4, 2, 1, 3, 0, 1, 2]; console.log(capturingRainwater(testArray)); // Leave this so that we can test your code: module.exports = capturingRainwater;

Hey, here’s my try at this. I used pointers and bounds for this one.

function capturingRainwater(heights) { let total = 0; let leftPointer = 0; let rightPointer = heights.length - 1; let leftBound = 0; let rightBound = 0; while (leftPointer < rightPointer) { if (heights[leftPointer] <= heights[rightPointer]){ if (heights[leftPointer] > leftBound) { leftBound = heights[leftPointer]; } total += leftBound - heights[leftPointer]; leftPointer++; } else { if(heights[rightPointer] > rightBound) { rightBound = heights[rightPointer]; } total += rightBound - heights[rightPointer]; rightPointer--; } } return total; } const testArray = [4, 2, 1, 3, 0, 1, 2]; console.log(capturingRainwater(testArray));

My code is bit longer. But seems to work too.

function capturingRainwater(heights) {

  // amount of captured water
  let water = 0;

  // looping each row (bottom to up)
  for(let i_row = 0; i_row < Math.max(...heights); i_row++) {

    // looping inside row, cell by cell (left to right)...
    // and finding out if cell is filled with water...
    for(let i_col = 0; i_col < (heights.length); i_col++) {

      // if current cell contains water...
      // then all following tests must result true

      // TEST 1/3
      // cell must not be "solid"
      let notSolid = ((heights[i_col] - i_row) > 0) ? false : true;
      if(notSolid===false) {continue} // skip rest of the code block

      // TEST 2/3      
      // cell must not be part outer column
      let notOuter = (i_col == 0 || i_col == (heights.length - 1)) ? false : true;
      if(notOuter===false) {continue} // skip rest of the code block
      
      // TEST 3/3
      // at least 1 solid cell must be present in each direction from current position (to left and to right)
      let isBuried = isCellBuried(heights, i_row, i_col);
      if(isBuried===false) {continue} // skip rest of the code block
      
      // above tests result true...
      // add 1 to amount of captured water
      water++;
    }  
  }
  return water;
}

// testing if given cell with given coordinates (rowId, colId) in given array of heights (heightsArr) is horizontally surrounded with at least one "solid cell" in each direction (all the way to the left and to the right)
function isCellBuried(heightsArr, rowId, colId) {
  
  // this array will map items in given row
  // items:
  //    solid block as true (solidBlock = true)
  //    non-block as false (solidBlock = false)
  let rowMap = [];
  
  // mapping solid and non-solid blocks inside array row
  heightsArr.forEach((item, index) => {
    let solidBlock = (item - rowId) > 0 ? true : false;
    rowMap.push(solidBlock);
  })

  // slicing the array row into two array parts (left and right)
  // given column from argument being delimiter
  // current cell and outer cells are not included
  let leftPart = rowMap.slice(0, colId);
  let rightPart = rowMap.slice((colId + 1), heightsArr.length);

  // testing if both of the parts contain at least one solid block
  // if yes, then current cell is able to contain water
  let cellInHole = (leftPart.includes(true) && rightPart.includes(true)) ? true : false;
  return cellInHole;
}

const testArray = [4, 2, 1, 3, 0, 1, 2];
console.log(capturingRainwater(testArray));

// Leave this so that we can test your code:
module.exports = capturingRainwater;
function capturingRainwater(heights) { // amount of captured water let water = 0; // looping each row (bottom to up) for(let i_row = 0; i_row < Math.max(...heights); i_row++) { // looping inside row, cell by cell (left to right)... // and finding out if cell is filled with water... for(let i_col = 0; i_col < (heights.length); i_col++) { // if current cell contains water... // then all following tests must result true // TEST 1/3 // cell must not be "solid" let notSolid = ((heights[i_col] - i_row) > 0) ? false : true; if(notSolid===false) {continue} // skip rest of the code block // TEST 2/3 // cell must not be part outer column let notOuter = (i_col == 0 || i_col == (heights.length - 1)) ? false : true; if(notOuter===false) {continue} // skip rest of the code block // TEST 3/3 // at least 1 solid cell must be present in each direction from current position (to left and to right) let isBuried = isCellBuried(heights, i_row, i_col); if(isBuried===false) {continue} // skip rest of the code block // above tests result true... // add 1 to amount of captured water water++; } } return water; } // testing if given cell with given coordinates (rowId, colId) in given array of heights (heightsArr) is horizontally surrounded with at least one "solid cell" in each direction (all the way to the left and to the right) function isCellBuried(heightsArr, rowId, colId) { // this array will map items in given row // items: // solid block as true (solidBlock = true) // non-block as false (solidBlock = false) let rowMap = []; // mapping solid and non-solid blocks inside array row heightsArr.forEach((item, index) => { let solidBlock = (item - rowId) > 0 ? true : false; rowMap.push(solidBlock); }) // slicing the array row into two array parts (left and right) // given column from argument being delimiter // current cell and outer cells are not included let leftPart = rowMap.slice(0, colId); let rightPart = rowMap.slice((colId + 1), heightsArr.length); // testing if both of the parts contain at least one solid block // if yes, then current cell is able to contain water let cellInHole = (leftPart.includes(true) && rightPart.includes(true)) ? true : false; return cellInHole; } const testArray = [4, 2, 1, 3, 0, 1, 2]; console.log(capturingRainwater(testArray)); // Leave this so that we can test your code: module.exports = capturingRainwater;