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 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' }); |
vancovver
commented
Aug 5, 2015
function where(collection, source) {
var arr = [];
for(var i=0; i<collection.length;i++){
if (collection[i].hasOwnProperty(Object.keys(source)))
var prop = Object.keys(source);
if (collection[i][prop]===source[prop]){
arr.push(collection[i]);}
}
return arr;
}
function where(collection, source) {
var arr = [];
var prop = Object.keys(source);
for(var key in collection){
if(collection[key].hasOwnProperty(Object.keys(source))) {
if (collection[key][prop] === source[prop]) {
arr.push(collection[key]);
}
}
}
return arr;
}
is all this codes are correct !!
its not working
function where(collection, source) {
var result = [];
var findKeys = Object.keys(source);
for (var i = 0; i < collection.length; i++) {
var hasProperties = true;
var currentObject = collection[i];
for (var j = 0; j < findKeys.length; j++) {
var key = findKeys[j];
if (!currentObject.hasOwnProperty(key) || currentObject[key] !== source[key]) {
hasProperties = false;
}
}
if (hasProperties) {
result.push(currentObject);
}
}
return result;
}
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