Skip to content

Instantly share code, notes, and snippets.

@sandrabosk
Created May 29, 2020 06:53
Show Gist options
  • Save sandrabosk/a7742e3b8ff43c18067f71b766ce4cdb to your computer and use it in GitHub Desktop.
Save sandrabosk/a7742e3b8ff43c18067f71b766ce4cdb to your computer and use it in GitHub Desktop.
// ***********************************************************************************************************************
// https://www.codewars.com/kata/filter-coffee
// You love coffee and want to know what beans you can afford to buy it.
// The first argument to your search function will be a number which represents your budget.
// The second argument will be an array of coffee bean prices.
// Your 'search' function should return the stores that sell coffee within your budget.
// The search function should return a string of prices for the coffees beans you can afford. The prices in this string
// are to be sorted in ascending order.
// ***********************************************************************************************************************
// solution 1:
function search(budget, prices) {
const resultArr = [];
prices.forEach(price => {
if (price <= budget) resultArr.push(price);
});
return resultArr.sort((a, b) => a - b).join();
}
// solution 2:
const search = (budget, prices) =>
prices
.filter(price => price <= budget)
.sort((a, b) => a - b)
.join();
// to test:
search(14, [7, 3, 23, 9, 14, 20, 7]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment