// Here's the explanation of the challenge -- when you're done reading it,
// click on `main.js` above to get started with the challenge!
// After a trip down to the furniture store, you've come back with... well,
// a random collection of pieces of wood and bits of metal, it seems.
// Apparently you have to assemble it yourself. Let's use code to see if we
// can make a dining space out of this.
// ## Description
// In this challenge, you will write a `maximumGuests` function that receives
// two arguments. The first argument is the number of wooden boards, and the
// second argument is the number of wooden legs. The arguments represent the
// pieces we can use to assemble dining tables and chairs.
// For example, if we have four wooden boards and ten wooden legs, the
// function could be invoked like:
// maximumGuests(4, 10)
// Your function should return the number of people that will be able to sit
// around the tables, once the furniture is assembled in the way that allows
// the most people to sit in it.
// For someone to be able to sit around a table, there has to be a **chair**
// for them to sit on, and a **space** in the table for them to sit in front
// of. Here's what you need to know about the furniture:
// To assemble a **table**, you will need **four wooden boards** and **four
// wooden legs**. Each table provides spaces to seat **four people** around
// them, provided there's a chair.
// To assemble a **chair**, you will need **one wooden board** and **three
// wooden legs**. Each chair can sit **one person**, provided there's space
// for them in a table.
// ## Example
// For example, if you have eight wooden boards and twelve wooden legs, you could
// assemble four chairs (with four boards to spare) but then no one would be able
// to sit around the table, because there wouldn't be any tables. You could also
// assemble two tables (with four legs to spare) but then no one would be able
// to sit around those tables either, because there wouldn't be any chairs.
// But with the same boards and legs, you can instead assemble a table and two
// chairs (with two boards and two legs to spare) and sit two people around a
// table. This is the most people you can sit around a table with that number
// of boards and legs, and therefore your function should return `2`.
/* Check out README.md for instructions! */
function maximumGuests(woodenBoards, woodenLegs) {
/* Your code goes here! */
/* Press [▶ Run] to see your code in action! */
/* You'll see the output of the tests below the button */
}
/* Test runner (do not modify this line!) */
const expect = require("./expect")(maximumGuests)
/* Test cases (remember to add more!) */
// no pieces, no furniture, no seats!
expect(0, 0, 0)
// with six boards and ten legs we can build
// a table and two chairs, seating two people
expect(2, 6, 10)
// your test case here!
// expect(expectedGuests, boards, legs)
/* 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`
)
}
}