INTERMEDIATE JAVASCRIPT MODULES : Named Imports - step 4


#1

Hello,

I’ve just finished this tutorial. I was wondering if someone could explain step 4 or point me to another example as to why this works the way it does. Here’s my code from missionControl.js:

// step 2
import {availableAirplanes, flightRequirements, meetsStaffRequirements} from './airplane';

// step 3
function displayStaffStatus() {
  // step 4 - see hint on left.  What does this do? 
  availableAirplanes.forEach(function(element){
  	// step 5 & step 6
    console.log(element.name + ' meets staff requirements: ' + meetsStaffRequirements(element.availableStaff, flightRequirements.requiredStaff) );  
  });
  
 // FYI: i have no idea what 'element' is doing above. 
  
}

// step 7
displayStaffStatus();

When i look at what the hint told me to do for step 4, i don’t understand what it is doing.

 availableAirplanes.forEach(function(element){
  1. what function is passed into availableAirplanes.forEach?
  2. what ‘element’ was passed in? Step 7 doesn’t pass in any perimeters. I’m confused.

For reference, here is the airplane.js code:

airplane.js

let availableAirplanes = [{
 name: 'AeroJet',
 fuelCapacity: 800,
 availableStaff: ['pilots', 'flightAttendants', 'engineers', 'medicalAssistance', 'sensorOperators'],
}, 
{name: 'SkyJet',
 fuelCapacity: 500,
 availableStaff: ['pilots', 'flightAttendants']
}];

let flightRequirements = {
  requiredStaff: 4,
};

function meetsStaffRequirements(availableStaff, requiredStaff) {
  if (availableStaff.length >= requiredStaff) {
    return true;
  } else {
    return false;
  }
};

export { availableAirplanes, flightRequirements, meetsStaffRequirements};

Thank you for your help.


#2

element is a temporary holder of each object in the availableAirplanes array as the method iterates over the array.

airplanes = [
    {
        prop: value
    },
    {
        prop: value
    },
    {
        prop: value
    }
]

Within the callback function body, element will have a value associated with element.prop.


#3

I’m seeing it again in later exercises. I understand why it is there (sort of). Hopefully it will click later. Thank you.