Created
February 21, 2015 17:56
-
-
Save aldraco/9f67eeec71fa81971b0e to your computer and use it in GitHub Desktop.
Where Art Thou bonfire challenge from Free Code Camp - two versions
This file contains hidden or 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
function where(collection, source) { | |
var arr = []; | |
var keyName = Object.keys(source)[0]; | |
collection.forEach(function(person) { | |
if (person.hasOwnProperty(keyName) && (person[keyName] === source[keyName])) { | |
//console.log(person[keyName]); | |
arr.push(person); | |
} | |
}); | |
console.log("1"+ JSON.stringify(arr)); | |
return arr; | |
} | |
//where([{ first: 'Romeo', last: 'Montague' }, { first: 'Mercutio', last: null }, { first: 'Tybalt', last: 'Capulet' }], { last: 'Capulet' }); | |
function whereTo(collection, source) { | |
var arr = []; | |
var keyName =Object.keys(source)[0]; | |
collection.map(function(person){ | |
if(person.hasOwnProperty(keyName) && (person[keyName] === source[keyName])) { | |
//console.log(person[keyName]); | |
arr.push(person); | |
} | |
}); | |
console.log("2"+ JSON.stringify(arr)); | |
return arr; | |
} | |
whereTo([{ first: 'Romeo', last: 'Montague' }, { first: 'Mercutio', last: null }, { first: 'Tybalt', last: 'Capulet' }], { last: 'Montague' }); |
here is my code:
`function where(collection, source) {
var check = true;
var arr = [];
for(i=0; i < collection.length; i++){
for (var prop in source){
if(collection[i].hasOwnProperty(prop) && collection[i][prop] == source[prop]){
check = true;
} else {
check = false;
break;
}
}
if (check === true){
arr.push(collection[i]);
}
}
return arr;
}`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
function where(collection, source) {
var result = [];
var findKeys = Object.keys(source);
for (var i = 0; i < collection.length; i++) {
}
return result;
}