function whatIsInAName(collection, source) {
var result = [];
var arr1=Object.keys(source);
console.log(arr1);
for(var i=0;i<collection.length;i++){
for(var j=0;j<arr1.length;j++){
if(collection[i].hasOwnProperty(arr1[j])===false){ //Check 1 if false go to next object in collection
break;
}
else if(collection[i].hasOwnProperty(arr1[j])){
console.log(source[arr1[j]],collection[i][arr1[j]])
if(source[arr1[j]]!==collection[i][arr1[j]]){ //Check 2 if value is not equal break loop and goto next object in collection
break;
}
continue; // if both check passes go for next property of source to check in object;
}
result.push(collection[i]); //if all values are present and checked in object push it in result array.
}
}
return result;
}
console.log(whatIsInAName([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }], { "a": 1, "b": 2 }));
Hey Guys I couldnt figure out the problem in my logic. I try to debug it even but cant find what the hell is a problem with logic.The program is to 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) Kindly help me over please.
What behavior are you seeing? How does it differ from what you expect?
edit: nevermind, read the code more closely.
the problem is its not pushing anything after every check.At first iteration its ‘a’ in nested loop. so if ‘a’ is not present in first array object so it donsnt need to check others it simply breaks the nested loop and go for next object to see in. if ‘a’ is present in object it will check value,if a key value of source is not equal to a key value of collection it will simply break the loop for that too and go for the next object to look in
we simply have two conditions that must be true the property and its value should be same in both source and collection. if any condition dont met inner loop breaks and go for the next object in collection array.
now after check the function should return array of objects which pass both test !!! in ur result array is empty. the whole problem is why its not pushing that object in result array which passes test.
You only get to the line result.push(collection[i]);
if your first if
– if(collection[i].hasOwnProperty(arr1[j])===false)
– is true.
Once you enter your else if(collection[i].hasOwnProperty(arr1[j]))
you will either hit a break
or continue
.
Generally speaking, you want to avoid break
and continue
unless you have a compelling reason. They result in what is called “spaghetti code”, which is very difficult to read, debug, and maintain. Those commands have their uses, but it’s generally recommended to try to organize you logic to have as few entry and exit points as is reasonable.
Comments unrelated to this issue:
- Logical evaluations (what you have inside an
if
) don’t need to be compared to true
or false
if they return a boolean (or otherwise truthy/falsy value). !
is the negation (or “not”) operator.
if(!false) { // i.e. if not false
return "always true";
}
- You have two
if
s that are mutually exclusive: (collection[i].hasOwnProperty(arr1[j])===false)
and if(collection[i].hasOwnProperty(arr1[j]))
.
If one is true then the other is necessarily false, so there is no need to use an else if
. A simple else
will do.
I get that but whats the problem in my code. I am checking all logics and passing every test that should be done. I debug the code and logics seem fine too but why its not pushing the object that passes test to result array …I just wanna know the error in my code. I want the same logic to run.There are 100 ways to solve problem. Thing is I want this way to work. There is some small logical error no on cant figure out.
If my first if will be true it will not go the line it will simply break the nested loop. Its simple if any condition is not met break the nested loop and go for the next object in collection. If both true push that object in result array.
You’re right. My bad. I forgot that first break. You have no way of ever getting to the push
because you always hit a break
or continue
.
How is that possible mate. we have two elements in arr1=[a,b]. at first iteration ‘a’ is present in collection first object so first test passes. value of ‘a’ in source and collection first object is same so this test also pass. Now continue to check the ‘b’ bcz we want ‘a’ and ‘b’ both and their value should also be same to ‘a’ and ‘b’ of collection first object so same tests run for ‘b’. That passes too. Now when it hit continue it sees that loop stop condition so loop we will come out of nested loop and push that collection object in result. Isnt that the flow of program my friend…
You will always enter either your if
or your else if
.
The if
will take you to a break
, so you won’t get to the push
line.
The else if
contains another if
. Inside that nested if
is a break
, so you don’t get to the push
line. Outside of that nested if
you will hit a continue
, so you don’t get to the push
line.
result.push(collection[i]);
is what we call “stranded code”. It is inaccessible. This is one of the risks of spaghetti code.
You mean that even when nested for loop is ended. Because of continue, it will not reach push whatsoever. It seems very strange. It’s difficult for me to believe it.
The push
is inside of the nested loop, so if you exit the inner loop early then you will not get to it.