Skip to content

a problem with a 6kyu level kata  #1935

@ghost

Description

Hey, I have a problem solving this kata

New Cashier Does Not Know About Space or Shift
Some new cashiers started to work at your restaurant.

They are good at taking orders, but they don't know how to capitalize words, or use a space bar!

All the orders they create look something like this:

"milkshakepizzachickenfriescokeburgerpizzasandwichmilkshakepizza"

The kitchen staff are threatening to quit, because of how difficult it is to read the orders.

Their preference is to get the orders as a nice clean string with spaces and capitals like so:

"Burger Fries Chicken Pizza Pizza Pizza Sandwich Milkshake Milkshake Coke"

The kitchen staff expect the items to be in the same order as they appear in the menu.

The menu items are fairly simple, there is no overlap in the names of the items:

  1. Burger
  2. Fries
  3. Chicken
  4. Pizza
  5. Sandwich
  6. Onionrings
  7. Milkshake
  8. Coke

there is my solution

function getOrder(input) {

	const arr1 = ['Burger ','Fries ','Chicken ','Pizza ','Sandwich ','Onionrings ','Milkshake ','Coke '];
	const arr2 = ['burger','fries','chicken','pizza','sandwich','onionrings','milkshake','coke'];
	
	arr1.forEach((el,i)=> {

		input=replaceAll(input,arr2[i],el)});

function replaceAll(str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
};

const arr3 = input.split(' ');
arr3.pop();

function count(array, what) {
let c = 0;
for (var i = 0; i < array.length; i++) {
if (array[i] === what) {
c++;
}
}
return c;
}

return ${'Burger '.repeat(count(arr3,'Burger'))}${'Fries '.repeat(count(arr3,'Fries'))}${'Chicken '.repeat(count(arr3,'Chicken'))}${'Pizza '.repeat(count(arr3,'Pizza'))}${'Sandwich '.repeat(count(arr3,'Sandwich'))}${'Onionrings '.repeat(count(arr3,'Onionrings'))}${'Milkshake '.repeat(count(arr3,'Milkshake'))}${'Coke '.repeat(count(arr3,'Coke') -1)}Coke;

}

some tests get passed and others show me this error
RangeError: Invalid count value
at String.repeat ()
at getOrder (test.js:37:323)
at Context. (test.js:74:25)

any ideas why this error occurs ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions