Solutions: Patatas bravas

Run Settings
LanguageJavaScript
Language Version
Run Command
// Here's the explanation of the challenge -- when you're done reading it, // click on `main.js` above to get started with the challenge! // It's time to make some _patatas bravas_ for our bar! But we just got a new // batch of potatoes in, and we're not sure how many portions we can make // using those potatoes. We'll figure this out with code! // ## Description // In this challenge, you will write a `makeBravas` function that receives a // list (array) as an argument. Each element of the list is a string // containing the word `"small"`, `"medium"` or `"large"`, representing a // potato of that size. // For example, if there is one large potato, one small potato and two medium // potatoes, your function could be invoked like: // makeBravas(["medium", "large", "small", "medium"]) // Your function should return the amount of complete portions of // _patatas bravas_ that can be made with the given potatoes. Here's all you // need to know about making _bravas_, the "secret recipe", if you will: // A **small** potato can only be chopped into **four** potato chips. // A **medium** potato can be chopped into **eight** potato chips. // A **large** potato can be chopped into **twenty-four** potato chips! // To make a **portion** of _patatas bravas_, you need **twenty** potato chips. // Any potato chips that remain after the complete portions are made are // discarded. For example, given one large potato, your function should return // exactly one (`1`), as you can only make one portion -- the four leftover // chips are ignored.
/* Check out README.md for instructions! */ // Procedural-style solution function proceduralMakeBravas(potatoes) { let chips = 0; potatoes.forEach((potato) => { if (potato == "small") { chips += 4 } else if (potato == "medium") { chips += 8 } else if (potato == "large") { chips += 24 } }) return Math.floor(chips / 20) } // Functional-style solution function functionalMakeBravas(potatoes) { // Reduce the list of potatoes, accumulating them // into portions and chips. // (See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) return potatoes.reduce(([portions, chips], potato) => { // Look up how many additional portions and leftover // chips can be obtained from this potato. const [morePortions, moreChips] = { small: [0, 4], medium: [0, 8], large: [1, 4], }[potato] portions += morePortions chips += moreChips // If we have accumulated more than twenty // leftover chips, make an additional portion // out of them. if (chips >= 20) { chips -= 20 portions += 1 } return [portions, chips] }, [0, 0])[0] } for (const solution of [ proceduralMakeBravas, functionalMakeBravas ]) { /* Test runner (do not modify this line!) */ const expect = require("./expect")(solution) /* Test cases (remember to add more!) */ // no potatoes, no bravas! expect(0, []) // one large potato gives you one portion expect(1, ["large"]) // leftovers are accumulated into portions expect(2, ["large", "medium", "small", "small"]) // your test case here! // expect(expectedPortions, potatoes) }
/* 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` ) } }
Editor Settings
Theme
Key bindings
Full width
Lines