Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clear&Readable solution in Clear category for [old] Treasures by SaintDron
"use strict";
function treasures(info, limit) {
limit *= 1000;
return Object.entries(info)
.sort((a, b) => b[1].price / b[1].weight - a[1].price / a[1].weight) // sort by worth
.map(([name, props]) => {
let count = Math.min(limit / props.weight, props.amount);
limit -= count * props.weight;
return (count) ? (name + ': ' + count) : ''; })
.filter(v => v)
.sort((a, b) => b[3] < a[3]); // sorting by the 4th character gives the required order
}
var assert = require('assert');
if (!global.is_checking) {
console.log('Example:')
console.log(treasures({'golden coin':
{'price': 100, 'weight': 50, 'amount': 200},
'silver coin':
{'price': 10, 'weight': 20, 'amount': 1000},
'ruby':
{'price': 1000, 'weight': 200, 'amount': 2}}, 5))
// These "asserts" are used for self-checking and not for an auto-testing
assert.deepEqual(treasures({'golden coin':
{'price': 100, 'weight': 50, 'amount': 200},
'silver coin':
{'price': 10, 'weight': 20, 'amount': 1000},
'ruby':
{'price': 1000, 'weight': 200, 'amount': 2}}, 5),
['golden coin: 92', 'ruby: 2'])
assert.deepEqual(treasures({'golden coin':
{'price': 100, 'weight': 50, 'amount': 100},
'silver coin':
{'price': 10, 'weight': 20, 'amount': 100},
'ruby':
{'price': 1000, 'weight': 200, 'amount': 1}}, 7.5),
['golden coin: 100', 'silver coin: 100', 'ruby: 2'])
console.log("Coding complete? Click 'Check' to earn cool rewards!");
}
Sept. 17, 2018
Comments: