I’m really stuck on this task; i’m still learning JavaScript objects so not my ideal question:
We need to figure out how many tables we need! The number of guests
may not divide evenly by the number of seats, so we will have to add
some extra chairs to a few of the tables for them.The function calculateTables takes two arguments, the number of guests
and the number of seats around a table.It should return an object with two properties: a key of tables with
the value of the number tables, and a key of remainingGuests with a
value of the number of guests without a seat who will need to be added
to one of the other tables.
Example log we shoudl try:
calculateTables(4, 2); // should return { tables: 2 , remainingGuests: 0 } calculateTables(14, 6); // should return { tables: 2 , remainingGuests: 2 } calculateTables(26, 5); // should return { tables: 5 , remainingGuests: 1 }
My code so far and i’m so stuck…..not sure where to go in solving this question. Any advice will be good:
function calculateTables(guests, seats) {
// Your code goes here...
let tables = 0;
let remainingGuests = 0;
for(let i=0; i<guests.length; i++){
if(guests % seats == 0){
tables = guests[i]
}
}
let obj ={'tables': tables, 'remainingGuests': remainingGuests}
return obj
}
2
Answers
You can use this code, i think it’s accurate to your expected result.
Am I overlooking something? This is just straight up division and modulo. The only catch I can see is that JS doesn’t have integer division, so you need to
floor
: