Skip to content

Instantly share code, notes, and snippets.

@wesscoby
Created August 7, 2019 06:34
Show Gist options
  • Save wesscoby/b86b89ebfd5f07dc1ddf0b04ba9c4bec to your computer and use it in GitHub Desktop.
Save wesscoby/b86b89ebfd5f07dc1ddf0b04ba9c4bec to your computer and use it in GitHub Desktop.
//! Exercise of the day
/**
* Some new cashiers started to work at our restaurant.
* All the orders they create look something like this:
*
** "milkshakepizzachickenfriescokeburgerpizzasandwitchmilkshakepizza"
*
* Their preference is to get the orders as a nice clean string with spaces and capitals like so:
*
** "Burger Fries Chicken Pizza Pizza Pizza Sandwitch Milkshake MilkShake Coke"
*
*? Write a Program to make this happen.
*/
let order = "milkshakepizzachickenfriescokeburgerpizzasandwichmilkshakepizza";
const format_order = orderString => {
// Predefined array of items on the menu
const itemsOnMenu = ['Burger', 'Chicken', 'Coke', 'Fries', 'Milkshake', 'Pizza', 'Sandwich'];
// Capitalize first letter of string
const capitalize = word => word.replace(/^\w/, firstLetter => firstLetter.toUpperCase());
let orderFormat = [];
// For every item in the predefined array, check for matches in orderString add result to orderFormat
itemsOnMenu.forEach(item => {
let pattern = RegExp(item, 'ig');
orderFormat.push(orderString.match(pattern).map(capitalize).join(" "));
})
return orderFormat.join(" ");
};
console.log(format_order(order));
//* Burger Chicken Coke Fries Milkshake Milkshake Pizza Pizza Pizza Sandwich
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment