// Here's the explanation of the challenge -- when you're done reading it,
// click on `main.js` above to get started with the challenge!
// Here at Nirvana Burgers, our burgers are very simple, but delicious --
// and yet, our new chef keeps messing them up somehow! We don't have time
// for another metaphysical kitchen debate on what constitutes a proper
// burger, so we'll have to settle the matter with code.
// ## Description
// In this challenge, you will write a `isProperBurger` function that receives
// a list (array) as an argument. Each element of the list is a string
// containing the name of an ingredient. Together, the list represents the
// whole stack of ingredients in the burger, ordered from bottom to top --
// in the order you'd put them on the plate.
// For example, for a simple cheese burger, your function could be invoked
// like:
// isProperBurger(
// ["bread", "patty", "cheese", "bread"])
// Your function should return a boolean (`true` or `false`) that represents
// whether the burger is properly made. Here's all you need to know about the
// art of making proper burgers:
// - A burger always starts and ends with `"bread"`. There should not be
// bread anywhere else in the burger.
// - There has to be **at least one** `"patty"` **in between** the two pieces
// of bread. If there's no patty, then it's not a burger, it's just a pile of
// bread.
// There cannot be `"cheese"` under the patties. Cheese always goes on top
// (that is, on the list, it must appear _after_ the patties)
// Our burgers really are simple -- you can be assured that every element in
// the list of ingredients will be one of the ingredients mentioned above.
/* Check out README.md for instructions! */
// Instruction-driven solution
function instructionIsProperBurger(ingredients) {
// A proper burger requires at least two pieces of bread and a patty,
// so if it has less than three elements, it can't be a burger.
if (ingredients.length < 3) {
return false
}
// If it doesn't start and end with bread, it's not a proper burger.
if (
ingredients[0] != "bread" ||
ingredients[ingredients.length - 1] != "bread"
) {
return false
}
let afterPatty = false
// Iterate through every ingredient between the bread.
for (let i = 1; i < ingredients.length - 1; i++) {
// If we find bread in between the bread, it's not
// a proper burger.
if (ingredients[i] == "bread") {
return false
}
// If we find cheese before the patty, it's not a proper burger.
if (ingredients[i] == "cheese" && !afterPatty) {
return false
}
// Keep track of whether we've already seen a patty, as it
// influences whether cheese is allowed or not.
if (ingredients[i] == "patty") {
afterPatty = true
}
}
// If we've made it this far, it must be a proper burger.
return true
}
// Outside-the-box solution
function outsideTheBoxIsProperBurger(ingredients) {
if (
// Check that the last ingredient is bread...
ingredients.pop() != "bread" ||
// ... and the first two ingredients are first bread,
// and then a patty.
// (If there cannot be cheese before the patty, or
// bread between the breads, then it must start with
// a patty)
ingredients.shift() != "bread" ||
ingredients.shift() != "patty"
) {
return false
}
// Check that every ingredient in between (after the
// patty and the bread are removed from the array) is
// not bread.
return ingredients.every(
(ingredient) => ingredient != "bread")
}
for (const solution of [
instructionIsProperBurger,
outsideTheBoxIsProperBurger
]) {
/* Test runner (do not modify this line!) */
const expect = require("./expect")(solution)
/* Test cases (remember to add more!) */
// no ingredients, no burger!
expect(false, [])
// a proper cheese burger
expect(true, ["bread", "patty", "cheese", "bread"])
// not a proper cheese burger
expect(false, ["bread", "cheese", "patty", "bread"])
// also a proper cheese burger
expect(true, ["bread", "patty", "patty", "cheese", "cheese", "bread"])
// your test case here!
// expect(expectedProper, burger)
}
/* Test runner (do not modify this!) */
module.exports = (run) => function expect(expected, ...inputs) {
const stringify = (value) => {
const serialised = JSON.stringify(value, undefined, 2)
|| "undefined"
const lines = serialised.split('\n')
for (i = 1; i < lines.length; i++) {
lines[i] = "\t" + lines[i]
}
return lines.join('\n')
}
const serialisedInputs = inputs
.map(stringify)
.join(", ")
const actual = run(...inputs)
if (expected !== actual) {
console.log(
` ❌ ${run.name}(${serialisedInputs})\n\n` +
`\texpected: ${stringify(expected)}\n` +
`\tbut instead got: ${stringify(actual)}\n\n`
)
} else {
console.log(
` ✅ ${run.name}(${serialisedInputs}) === ${stringify(actual)}\n`
)
}
}