Solution Review: Counting Population

Solution overview of the “Counting Population” exercise.

We'll cover the following

Solution

After setting up loops to iterate the entire two-dimensional array, the solution relies on visualizing the array and ensuring that correct indices are accessed. First create nested loops to iterate the given two-dimensional array. Set terminating conditions to the size of the arrays so that no invalid index is accessed. After setting it up, access each object. Since the problem requires us to take the sum of population with temperature less than or equal to 10°C, we only need to look at the temp and population properties of the object. For each element we do the following:

  • If the element value is equal to null, ignore it.
  • If the object’s temp property is assigned a value greater than 10, ignore it.
  • Otherwise, for an object with temp property assigned to a value less than or equal to 10, add the population property value to the ans variable.

Once we set up our conditionals for each element, our program will work as required.

Get hands-on with 1400+ tech skills courses.