Before becoming a Marine, you have to be a recruit. Recruits are people who enter Marine Corps boot camp aspiring to be Marines. In order to earn the coveted Eagle, Globe and Anchor emblem, the hallmark of becoming a Marine, Marines must pass the most rigorous boot camp the U.S. Military has to offer: The Marine Corps Boot camp. It includes passing the mental and physical evolution known as the crucible, earning a marksmanship tier, and passing the final physical fitness test.

Let’s say we have an array of four recruits. We know that the Marine Corps only accepts recruits who have passed the above-mentioned tests. Here’s how you show that in the language of JavaScript.

// Here we have a list of recruits and their data.

// Each recruit is stored in between curly brackets to denote a javascript object. let recruits =[

{

name: "Matin",

pftScore: 300,

rifleScore: 280,

passedCrucible: true

},

{

name: "Predo",

pftScore: 280,

rifleScore: 295,

passedCrucible: true

},

{

name: "Tsoy",

pftScore: 300,

rifleScore: 350,

passedCrucible: true

},

{

name: "Shmuckatelli",

pftScore: 100,

rifleScore: 100,

passedCrucible: false

}

]; We then store the objects inside an array which we later use to iterate through.

Let’s write a program to iterate all these recruits and filter out who has earned the title.

//This lengthy code block below is known as a for-loop. let makeMarines =[]; for(let i=0; i <recruits.length; i++){ if(recruits[i].passedCrucible) makeMarines.push(recruits[i]); } console.log('this is marines',makeMarines)

console.log('this is coastguard', sendToCoastGuard) /* makeMarines = [ {

name: "Matin",

pftScore: 300,

rifleScore: 280,

passedCrucible: true

},

{

name: "Predo",

pftScore: 280,

rifleScore: 295,

passedCrucible: true

},

{

name: "Tsoy",

pftScore: 300,

rifleScore: 350,

passedCrucible: true

}; */

Instead of using this complicated looking code, we could use a filter method to write a more efficient coded function.

Filter is an array method so we will start with the array of recruits. It uses a callback function that runs on each element in the array. It uses a return statement to show which elements will actually end up in the final array, in this case, the earnedTheTitleMarines

const makeMarines = recruits.filter(recruit => {

return recruit.pftScore >= 110 &&

recruit.rifleScore >= 190 &&

recruit.passedCrucible === true

}) const sendToCoastGuard = recruits.filter(recruit => {

return recruit.pftScore < 110 ||

recruit.rifleScore < 190 ||

recruit.passedCrucible === false

}) /* makeMarines = [ {

name: "Matin",

pftScore: 300,

rifleScore: 280,

passedCrucible: true

},

{

name: "Predo",

pftScore: 280,

rifleScore: 295,

passedCrucible: true

},

{

name: "Tsoy",

pftScore: 300,

rifleScore: 350,

passedCrucible: true

}; */

The inputs and outputs are the same, so here’s what we did differently:

We didn’t need to declare the makeMarines array and then fill it later. We declared it and then filled it with elements in the same code block We actually used a condition within the return statement. That means that we only return elements that pass a certain condition. We can now use recruits for each element in the array, rather than recruits[i] as we did in the for loop.

There is one important difference when compared to for loops. If you check out the makeMarines variable, you can see that it will only evaluate to true or false . Then, that boolean is fed into the return statement.

So, that true or false really just decides if each member of the original array will be included or not in the resulting array, makeMarines