I was attempting to write some code that checks if two individuals share the same birthday. Person "a" and person "b" do not have the same birthday, yet the console output shows:
a was born on day 1
a has the same birthday as a
a has the same birthday as b
b was born on day 2
b has the same birthday as a
b has the same birthday as b
when it should read:
a was born on day 1
a has the same birthday as a
b was born on day 2
b has the same birthday as b
Here's the code snippet:
var people = {
a: {
name: "a",
birthday: 1,
},
b: {
name: "b",
birthday: 2,
}
};
for(var x in people) {
console.log(people[x].name + " was born on day " + people[x].birthday)
for(var y in people) {
if(people[x].birthday = people[y].birthday) {
console.log(people[x].name + " has the same birthday as " + people[y].name)
}
}
}
people[x].birthday = people[y].birthday
The issue seems to stem from this line of code.