Translating Marine Corps Bootcamp using JavaScript higher order functions

Jameel Matin
4 min readAug 21, 2018
Photo credit: Getty Images

Hello Freakin daggon! Good afternoon, Marines! My name is Sergeant Matin and I will be conducting the next period of instruction on how JavaScript functions can be explained by going through Marine Corps boot camp.

If you’re in the Marines now, particularly a grunt, you may not have heard about JavaScript. I remember when I was in, I was never really exposed to very many civilian careers other than criminal justice type paths offered by some mickey mouse for-profit institution like University of Phoenix, American Military University, or Trump University.

Thus I had to find my passion through trial and error after leaving service. If you want to have a healthy fulfilling civilian life after your military service, it’ll behoove you to start thinking about your civilian life while you’re in.

I suggest you look into a career in an industry that provides a respectable stable income, such as the STEM field. You can read more on the subject here.

Here’s a parody video of what it feels like to transition out of the Marine Corps.

What it’s like for millennial veterans to go from one system to another system.

Before becoming a Marine, you have to be a recruit. Recruits are people endeavoring to earn the coveted Eagle, Globe, and Anchor emblem, the hallmark of becoming a Marine.

Recruits look to 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.

  1. Filter is an array method so we will start with the array of recruits.
  2. It uses a callback function that runs on each element in the array.
  3. 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:

  1. 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
  2. We actually used a condition within the return statement. That means that we only return elements that pass a certain condition.
  3. 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

Rah

--

--