Created
August 12, 2017 23:23
-
-
Save bouyagas/18208dd85d637735583ceb4dc43c6fd5 to your computer and use it in GitHub Desktop.
null created by bouyagas - https://repl.it/KHbM/5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var youDontNeedToWorryAboutAnythingInHere = function() { | |
var NameArray = ['Noel Plain', 'Steffanie Plain', 'Hester Sanderfur', 'Retta Plain', 'Idella Saur','Shila Plain', "Marybeth Peavler", "Chadwick Jin", "Jerold Chauez", "Roselle Plain", "Doria Figgins", "Seth Arvizu", 'Gloria Wyche', "Melodi Plain", "Robin Shackleford", "Jack Plain", "Marina Prost", "Melvina Plain", "Ryan Plemons", "Janis Ohare", "Assata Shakur", "Kanye West", "Jay-Z", "Mariah Carey", "Bianca Gandolfo", "Cedric the Entertainer", "Chloe Plain"]; | |
var JobArray = ['Mortician', 'Broadcaster', 'Craftsperson', 'Engineer', 'Interior designer', 'Nun']; | |
var CityArray = ["Scottsdale, Arizona", "Oakland, California", "Stockton, California", "New York, NY"]; | |
var emptyNetwork = []; | |
function createNetwork(collection){ | |
for (var i = 0; i < NameArray.length; i++) { | |
var randomJob = JobArray[Math.floor(Math.random() * JobArray.length)]; | |
var randomCity = CityArray[Math.floor(Math.random() * CityArray.length)]; | |
collection.push(createNewUser(NameArray[i], randomJob, randomCity)); | |
} | |
return collection | |
} | |
return createNetwork(emptyNetwork); | |
} | |
function createNewUser(username, job, city){ | |
var newUser = { | |
username: username, | |
job: job, | |
city: city, | |
friends: [], | |
family: [], | |
coworkers: [], | |
}; | |
return newUser; | |
} | |
// creating the user you're going to be adding to | |
var joeyProfile = createNewUser('Joey Plain', 'Engineer', 'Oakland, California'); | |
// creating the network collection you're going to be pulling | |
var allUsers = youDontNeedToWorryAboutAnythingInHere(); | |
// check the console to see what these look like | |
// console.dir(allUsers); | |
// console.dir(ourUser); | |
//console.dir(userNetwork); | |
// YOUR CODE HERE | |
//original version, with floops | |
// for (var i = 0; i < allUsers.length; i++) { | |
// var user = allUsers[i]; | |
// var lastName = user.username.split(' ')[1]; | |
// if (user.city === joeyProfile.city) { | |
// joeyProfile.friends.push(user.username); | |
// } | |
// if (user.job === joeyProfile.job) { | |
// joeyProfile.coworkers.push(user.username); | |
// } | |
// if (lastName === joeyProfile.username.split(' ')[1]) { | |
// joeyProfile.family.push(user.username); | |
// } | |
// } | |
//populate friends (no family) | |
//populate coworkers (no friends or family) | |
//populate family | |
//myObj.hasOwnProperty('banana'); //-> true or false | |
//myArr.indexOf(value); //-> -1 if it is not there, orsomething between 0 and myArr.length - 1 if it is | |
//myArr.includes(value); //-> true or false | |
//loop through family array | |
//if a family member is in friends array, remove it from friends array | |
// for (var i = 0; i < joeyProfile.family.length; i++) { | |
// var familyMember = joeyProfile.family[i]; | |
// for (var j = 0; j < joeyProfile.friends.length; j++) { | |
// if (familyMember === joeyProfile.friends[j]) { | |
// joeyProfile.friends.splice(j, 1); | |
// j--; | |
// } | |
// } | |
// } | |
// for (var i = 0; i < joeyProfile.friends.length; i++) { | |
// var friend = joeyProfile.friends[i]; | |
// for (var j = 0; j < joeyProfile.coworkers.length; j++) { | |
// var coworker = joeyProfile.coworkers[j]; | |
// if (coworker === friend) { | |
// joeyProfile.coworkers.splice(j, 1); | |
// j--; /*is the same as j = j - 1; */ | |
// } | |
// } | |
// } | |
//[1, 2, 3] | |
//i is 1 | |
//(the array).splice(i, 1) | |
//[1, 3] | |
//i--; | |
//refactor with HOFs | |
joeyProfile.family = allUsers.filter(function(user) { | |
var lastname = user.username.split(' ')[1]; | |
return lastname === 'Plain'; | |
}).map(function(user) { | |
return user.username; | |
}); | |
joeyProfile.friends = allUsers.filter(function(user) { | |
return user.city === joeyProfile.city; | |
}).map(function(user) { | |
return user.username; | |
}).filter(function(friend) { | |
return joeyProfile.family.indexOf(friend) === -1; | |
}); | |
joeyProfile.coworkers = allUsers.filter(function(user) { | |
return user.job === joeyProfile.job; | |
}).map(function(user) { | |
return user.username; | |
}).filter(function(coworker) { | |
var isNotFamily = joeyProfile.family.indexOf(coworker) === -1; | |
var isNotFriend = joeyProfile.friends.indexOf(coworker) === -1; | |
return isNotFamily && isNotFriend; | |
}); | |
//check results | |
console.dir(joeyProfile); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment