← Back to challenges

Making a Sandwich

JavaScriptHardarraysloops

Instructions

Given an array of ingredients i and a string flavour f as input, create a function that returns the array but with the elements bread around the selected ingredient.

Examples

makeSandwich(["tuna", "ham", "tomato"], "ham") ➞ ["tuna", "bread", "ham", "bread", "tomato"]

makeSandwich(["cheese", "lettuce"], "cheese") ➞ ["bread", "cheese", "bread", "lettuce"]

makeSandwich(["ham", "ham"], "ham") ➞ ["bread", "ham", "bread", "bread", "ham", "bread"]

Notes

  • You will always get valid inputs.
  • Make two separate sandwiches if two of the same elements are next to each other (see example #3).
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Remove Repeated Letters