Skip to content

Instantly share code, notes, and snippets.

@sethschori
Forked from anonymous/Civil War (3.4).js
Created July 31, 2016 19:48
Show Gist options
  • Save sethschori/6c7d277c9dce79f0e85616ab252e5fa0 to your computer and use it in GitHub Desktop.
Save sethschori/6c7d277c9dce79f0e85616ab252e5fa0 to your computer and use it in GitHub Desktop.
https://repl.it/CQr3/235 created by sethopia
/*
MARVEL CIVIL WAR
Given an Array of superhero objects, create a function that check if the heroes with a true "is_registered" property also have a "secret_identity". If they are unregistered, or registered without their secret identity, add them to a "Government Watch" array and log their "testify" functions to the console.
*/
//Superhero Super-Class Constructor
var Superhero = function(name, identity, registered) {
var hero = {
hero_name: name,
secret_identity : identity,
is_registered: registered,
testify : function() {
return "My name is " + this.hero_name;
}
}
return hero;
}
var ironMan = new Superhero('Iron Man', 'Tony Stark', true);
var spiderMan = new Superhero('Spider Man', 'Peter Parker', false);
var hawkeye = new Superhero('Hawkeye', null, false);
var winterSoldier = new Superhero('Winter Soldier', 'Bucky Barnes', false);
var captainAmerica = new Superhero('Captain America', 'Steve Rogers', false);
var theVision = new Superhero('The Vision', null, true);
var blackWidow = new Superhero('Black Widow', 'Natasha Romanoff', false);
var blackPanther = new Superhero('Black Panther', "T'Challa", true);
var civilWar = [ironMan, spiderMan, hawkeye, winterSoldier, captainAmerica, theVision, blackWidow, blackPanther];
function checkSuperheroes(arr) {
var governmentWatch = [];
for (var i = 0; i < arr.length; i++) {
var thisSuperhero = arr[i];
if (!thisSuperhero.is_registered) {
governmentWatch.push(thisSuperhero);
console.log(thisSuperhero.testify());
} else if (thisSuperhero.is_registered && !thisSuperhero.secret_identity) {
governmentWatch.push(thisSuperhero);
console.log(thisSuperhero.testify());
/*
NOTE: A shorter alterative to the if / else above would be a single if:
if (!thisSuperhero.is_registered || !thisSuperhero.secret_identity)
This works because the second OR condition will only be evaluated
if the first condition fails.
*/
}
}
return governmentWatch;
}
var governmentWatchList = checkSuperheroes(civilWar);
console.log(governmentWatchList);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment