I'm currently working on developing a function that will activate when the count either matches or is half the initial price required to open, and it should not reset to false even if the count drops back to 0.
Below is some sample data:
openData = {
count: 0,
chest: [
{ id: 'Chest1', price: 50, open: false },
{ id: 'Chest2', price: 200, open: false },
{ id: 'Chest3', price: 500, open: false }
]
};
("changes `open` to `true` when the count meets or exceeds half of the initial price of the corresponding chest", function() {
openData.count = 100;
code.unlockedChest(openData.chest, openData.count);
expect(openData.chest[0].open).to.equal(true);
expect(openData.chest[1].open).to.equal(true);
expect(openData.chest[2].open).to.equal(false);
});
('keeps `open` as `true` once a chest has been successfully opened, regardless of count dropping back to 0', function() {
openData.count = 100;
code.unlockedChest(openData.chest, openData.count);
openData.count = 0;
code.unlockedChest(openData.chest, openData.count);
expect(openData.chest[0].open).to.equal(true);
expect(openData.chest[1].open).to.equal(true);
expect(openData.chest[2].open).to.equal(false);
});
});
Here is what I have come up with so far:
function unlockedChest (id, count) {
if (count === chest.price || count <= chest.price) {
return chest.open === true;
}
if (chest.open === true) {
return true; //trying to maintain the changed value
}
}
I've spent several hours experimenting with this, but I believe my logic might be flawed, especially due to the interpretation of 'half'. The current output I am receiving expects false to be true for the first specification.
I am also considering the possibility of implementing a loop. Any additional advice on both specifications would be greatly appreciated!