get help in general - we have more specialized help rooms here: https://gitter.im/FreeCodeCamp/home
function whatIsInAName(collection, source) {
// What's in a name?
// Only change code below this line
var ArrWithSource = collection.filter((function(objects){
// Return the array that has the following condition:
var sourceProp = source[Object.keys(source)];
// Property of the source key in source args
var sourceKey = [Object.keys(source)];
// Key of the source value in collection args
if (sourceProp != sourceKey) {
return false;
} else { return true;}
return objects[sourceKey] === sourceProp;
// Filter the array where the requested property is equal to the one inside the source argument
}));
return ArrWithSource;
}
// Only change code above this line
whatIsInAName([{ "a": 1, "b": 2 },
{ "a": 1 },
{ "a": 1, "b": 2, "c": 2 }],
{ "a": 1, "b": 2 });
function whatIsInAName(collection, source) {
// What's in a name?
// Only change code below this line
var ArrWithSource = collection.filter((function(objects){
// Return the array that has the following condition:
var sourceProp = source[Object.keys(source)];
// Property of source args
var sourceKey = [Object.keys(source)];
// Key of the source value in collection args
var i = -1;
while (i < sourceKey[0].length-1) {
i++;
return objects[sourceKey[0][i]] === sourceProp;
}
// Filter the array where the requested property is equal to the one inside the source argument
}));
return ArrWithSource;
}
// Only change code above this line
whatIsInAName([{ "a": 1, "b": 2 },
{ "a": 1 },
{ "a": 1, "b": 2, "c": 2 }],
{ "a": 1, "b": 2 });
thekholm80 sends brownie points to @darrenfj :sparkles: :thumbsup: :sparkles:
:star2: 2133 | @darrenfj |http://www.freecodecamp.com/darrenfj
georgiawang5332 sends brownie points to @moigithub :sparkles: :thumbsup: :sparkles:
:star2: 3671 | @moigithub |http://www.freecodecamp.com/moigithub
ericmiller777 sends brownie points to @darrenfj :sparkles: :thumbsup: :sparkles:
:star2: 2134 | @darrenfj |http://www.freecodecamp.com/darrenfj
manish-giri sends brownie points to @heroiczero and @daddycardona and @darrenfj and @thekholm80 :sparkles: :thumbsup: :sparkles:
:cookie: 390 | @daddycardona |http://www.freecodecamp.com/daddycardona
:star2: 1509 | @thekholm80 |http://www.freecodecamp.com/thekholm80
:star2: 1882 | @heroiczero |http://www.freecodecamp.com/heroiczero
:star2: 2135 | @darrenfj |http://www.freecodecamp.com/darrenfj
manish-giri sends brownie points to @teddyhugs :sparkles: :thumbsup: :sparkles:
:cookie: 327 | @teddyhugs |http://www.freecodecamp.com/teddyhugs
teddyhugs sends brownie points to @manish-giri :sparkles: :thumbsup: :sparkles:
:star2: 6653 | @manish-giri |http://www.freecodecamp.com/manish-giri
heroiczero sends brownie points to @daddycardona and @darrenfj and @thekholm80 and @manish-giri :sparkles: :thumbsup: :sparkles:
:star2: 1510 | @thekholm80 |http://www.freecodecamp.com/thekholm80
:cookie: 391 | @daddycardona |http://www.freecodecamp.com/daddycardona
:star2: 2136 | @darrenfj |http://www.freecodecamp.com/darrenfj
:star2: 6654 | @manish-giri |http://www.freecodecamp.com/manish-giri
var Person = function(firstAndLast) {
var name = firstAndLast;
this.getFullName = function() {
return "name";
};
this.getLastName = function() {
name = name.split(" ");
return name[1];
};
this.getFirstName = function() {
name = name.split(" ");
return name[0];
};
this.setFirstName = function(str) {
name.split(" ");
name[0] = str;
name.join(" ");
return name;
};
return firstAndLast;
};
var bob = new Person('Bob Ross');
bob.setFirstName(dan); //returns error at this line saying "dan is not defined"
bob.getFullName();
bob.setFirstName("dan")
rscales02 sends brownie points to @pethaf :sparkles: :thumbsup: :sparkles:
:cookie: 427 | @pethaf |http://www.freecodecamp.com/pethaf
bob.setFirstName("dan")
it returns "dan ross"
but then when I run bob.getFullName()
immediately after it returns name
any idea why?
github-henry sends brownie points to @darrenfj :sparkles: :thumbsup: :sparkles:
:star2: 2137 | @darrenfj |http://www.freecodecamp.com/darrenfj
this.getFullName = function() {
return "name";
};
true,
false,
true
burinson sends brownie points to @alpox :sparkles: :thumbsup: :sparkles:
:star2: 1361 | @alpox |http://www.freecodecamp.com/alpox
function foo() {
return 5;
}
foo(); // Function call
function foo() {
var bar = "baz";
return bar;
}
bar
from the function foo
var newVar = foo();
console.log(newVar);
baz
function whatIsInAName(collection, source) {
var ArrWithSource = collection.filter((function(objects){
for (var i = 0; i < [Object.keys(source)][0].length; i++) {
// For checking each source property
var sourceKey = [Object.keys(source)];
// Key of the source arguments. Ex: a, b
var sourceProp = source[sourceKey[0][i]];
// Property of source args. Ex: source.a = 1
console.log(objects[sourceKey[0][i]] === sourceProp);
// Filter the array where the requested property is equal to the one inside the source argument
}
}));
return ArrWithSource;
}
whatIsInAName([{ "a": 1, "b": 2 },
{ "a": 1 },
{ "a": 1, "b": 2, "c": 2 }],
{ "a": 1, "b": 2 });
objects[sourceKey[0][i]] === sourceProp
inside a variable
[Object.keys(source)][0]
is totally redundant. Its the same as just: Object.keys(source)
var sourceKey = [Object.keys(source)];
that sure does not give you a key, it gives you an array with one element - another array which holds all keys of the source.
Object.keys(source)[i]
to get the key at index i
source[sourceKey]
sourceProp
with your code too, but you make a long way around :D
function whatIsInAName(collection, source) {
var ArrWithSource = collection.filter((function(objects){
for (var i = 0; i < [Object.keys(source)].length; i++) {
// For checking each source property
var sourceKey = [Object.keys(source)[i]];
// Key of the source arguments. Ex: a, b
var sourceProp = source[sourceKey[0][i]];
// Property of source args. Ex: source.a = 1
console.log(source[sourceKey]);
// Filter the array where the requested property is equal to the one inside the source argument
}
}));
return ArrWithSource;
}
whatIsInAName([{ "a": 1, "b": 2 },
{ "a": 1 },
{ "a": 1, "b": 2, "c": 2 }],
{ "a": 1, "b": 2 });
[]
around Object.keys(source)
?
If you do:
var foo = [1, 2, 3, 4];
var bar = [foo];
What is the value of bar
?
Object.keys(source)
so you always needed to go through [0]
3
how would you access it?
so if you would instead do:
var bar = foo; // Without the []
you could do:
bar[2]
Directly
function whatIsInAName(collection, source) {
var ArrWithSource = collection.filter((function(objects){
for (var i = 0; i < Object.keys(source).length; i++) {
// For checking each source property
var sourceKey = Object.keys(source)[i];
// Key of the source arguments. Ex: a, b
var sourceProp = source[sourceKey];
// Property of source args. Ex: source.a = 1
console.log(objects[sourceKey] === sourceProp);
// Filter the array where the requested property is equal to the one inside the source argument
}
}));
return ArrWithSource;
}
whatIsInAName([{ "a": 1, "b": 2 },
{ "a": 1 },
{ "a": 1, "b": 2, "c": 2 }],
{ "a": 1, "b": 2 });
Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching property and value pairs (second argument). Each property and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.
Each property and value pair of the source object has to be present in the object
you have to loop through all of the source properties before you can tell if they are all there or not.
false
case and return true
after the loop.
var everyIsTrue = true;
for(...) {
if(...) {
everyIsTrue = false;
}
}
return everyIsTrue;
for(...) {
if(...) {
return false;
}
}
return true;
...looks pretty much the same i wrote yesterday :P
one way
loop on all sources if (source prop !== collection prop) return false; //one prop dont match return true; //all matches
anther way is using .every method
.every
but i newcomers have hard times with such things :D
function whatIsInAName(collection, source) {
var ArrWithSource = collection.filter((function(objects){
for (var i = 0; i < Object.keys(source).length; i++) {
// For checking each source property
var sourceKey = Object.keys(source)[i];
// Key of the source arguments. Ex: a, b
var sourceProp = source[sourceKey];
// Property of source args. Ex: source.a = 1
if (objects[sourceKey] != sourceProp) {return false;}
// Filter the array where the requested property is equal to the one inside the source argument
} return true;
}));
return ArrWithSource;
}
whatIsInAName([{ "a": 1, "b": 2 },
{ "a": 1 },
{ "a": 1, "b": 2, "c": 2 }],
{ "a": 1, "b": 2 });
burinson sends brownie points to @moigithub :sparkles: :thumbsup: :sparkles:
:star2: 3672 | @moigithub |http://www.freecodecamp.com/moigithub