Challenge 5 Megathread

Welcome to challenge 5! Use this thread for any and all questions relating to challenge 5.

Plus, we have a handy walk-through video from Lighthouse Labs instructor Nima and education intern David. Watch it below. :eyes:


Using filter and map

const chooseStations = stations => stations
  .filter(([name,capacity,type]) => capacity >= 20 && (type === "school" || type === "community centre"))
  .map(station => station[0])

remember to use the && and || notations carefully so you know which one gets compared first (:

1 Like

but I think you will still have to loop through the array to construct a new array of only names, unlike challenge 4 when only filter is needed,

hi, i followed this step, but it seems the system is detecting errors in it.
if i click run test, it passes but if i click on submit code, it shows errors. my code is below

const chooseStations = (stations) => {
//empty array that will hold the selected stations
let selectedStations = [];

//loop through the stations
for (let station of stations){

//assigning station index variable names
const name = station[0];
const capacity = station[1];
const type = station[2];

//using if statement to check the capacity
if(capacity >= 20){

  //using OR operator to check if the station is a school or a community center
  if(type === "school" || "community center"){
    selectedStations.push(name) //adding the selected stations to the selectedStations array;

console.log( selectedStations)
return selectedStations;

 the error message is below:   

Failed: The chooseStations should return only the appropriate stations.

Reason: Your code ran but did not produce the correct result.

Hint: Make sure you're filtering the array properly.

above is a screenshot of my code

Make sure you test against the Canadian spelling of “community centre” not “center”.

Or, expand your code slightly to include both spellings.


Also, while this challenge can be completed in a single line of code (using filter and map), your methodical approach is very clear and readable.

One thing you might want to add to your toolkit is Array Destructuring ( which might clean up the code a tiny bit:

for ([name, capacity, type] of stations){ ... }

All tests pass successfully but I get an" Failed: The chooseStations should return only the appropriate stations." when submitting the code. Is this a bug?


thank you, i was able to get it passed using the destructuring method. thank you

I'm trying to resolve but print Undefined 

   const chooseStations = (stations) => {
  // Code here!

  let goodStation = [];
  for (let station in stations){
      const name = stations[0];
      const type = station[2];
      const capacity = station[1];
    if (capacity > 20  && (type === "school" || type ==="community centre")){
  // Remember to return a value!
  return goodStation;

TEST CODE passes,
console logged output matches output,
SUBMIT CODE fails: Failed: The chooseStations should return only the appropriate stations.
Is this a bug, or am I missing something here?

I was going blind. (I couldn’t test my code). All you need is to use so
stations.filter(/* stuff goes here*/).map(station => station[0])

Hi RaVCOder, I haven’t tested your code, but try a couple of these changes to see if it fixes it:

  • const name = station[0] instead of stations
  • I would move the push line inside the if statement, since you only want to push the names where all the conditions are met; right now because it’s outside the if statement it will take all the names

you are using an assignment ‘=’ instead of == or === comparison operator (in filter method)

Tried ‘=’ vs ‘==’ vs ‘===’ same result.

“centre”, not “center”

Same here, the tests runs successfully but submitting it gives me the same error as you.