Last active
May 20, 2019 18:26
-
-
Save rinterliche/0c7dc02ae0f9d6715118037dccab1f6d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// ====================================== | |
// Gilded Rose Requirements Specification | |
// ====================================== | |
// We are a small inn with a prime location in a prominent city ran by a friendly innkeeper named Allison. | |
// We also buy and sell only the finest goods. | |
// Unfortunately, our goods are constantly degrading in quality as they approach their sell by date. We | |
// have a system in place that updates our inventory for us. | |
// First an introduction to our system: | |
// - All items have a SellIn value which denotes the number of days we have to sell the item | |
// - All items have a Quality value which denotes how valuable the item is | |
// - At the end of each day our system lowers both values for every item | |
// Pretty simple, right? Well this is where it gets interesting: | |
// - The Quality of an item is never negative; | |
// - "Aged Brie" actually increases in Quality the older it gets; | |
// - The Quality of an item is never more than 50; | |
// - "Backstage passes", like "Aged Brie", increases in Quality as its SellIn value approaches; | |
// - "Sulfuras", being a legendary item, never has to be sold or decreases in Quality. | |
// Just for clarification, an item can never have its Quality increase above 50, however "Sulfuras" is a | |
// legendary item and as such its Quality is 80 and it never alters. | |
// Suppose you have the following data amostration for items: | |
var data = [ | |
{ | |
name: '+5 Dexterity Vest', | |
sell_in: 10, | |
quality: 20 | |
}, { | |
name: 'Aged Brie', | |
sell_in: 2, | |
quality: 0 | |
}, { | |
name: 'Elixir of the Mongoose', | |
sell_in: 5, | |
quality: 7 | |
}, { | |
name: 'Sulfuras', | |
sell_in: 0, | |
quality: 80 | |
}, { | |
name: 'Backstage Passes', | |
sell_in: 15, | |
quality: 20 | |
}, { | |
name: 'Conjured Mana Cake', | |
sell_in: 3, | |
quality: 6 | |
} | |
]; | |
// The proposed solution is: | |
function update_quality(items) { | |
for (var i = 0; i < items.length; i++) { | |
if (items[i].name != 'Aged Brie' && items[i].name != 'Backstage passes') { | |
if (items[i].quality > 0) { | |
if (items[i].name != 'Sulfuras') { | |
items[i].quality = items[i].quality - 1; | |
} | |
} | |
} else { | |
if (items[i].quality < 50) { | |
items[i].quality = items[i].quality + 1; | |
if (items[i].name == 'Backstage passes') { | |
if (items[i].quality < 50) { | |
items[i].quality = items[i].quality + 1; | |
} | |
} | |
} | |
} | |
if (items[i].name != 'Sulfuras') { | |
items[i].sell_in = items[i].sell_in - 1; | |
} | |
if (items[i].sell_in < 0) { | |
if (items[i].name != 'Aged Brie') { | |
if (items[i].name != 'Backstage passes') { | |
if (items[i].quality > 0) { | |
if (items[i].name != 'Sulfuras') { | |
items[i].quality = items[i].quality - 1; | |
} | |
} | |
} else { | |
items[i].quality = items[i].quality + 1; | |
} | |
} else { | |
if (items[i].quality < 50) { | |
items[i].quality = items[i].quality + 1; | |
} | |
} | |
} | |
} | |
} | |
update_quality(data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment