# Thread: Calculation help

1. ## Calculation help

I have 2 values travelers and rooms. I need to calculate the people per room. So if rooms = 1 then travelers = travelers. But if rooms is greater than 1, I need to distribute the travelers evenly across the rooms.

Example
rooms 1 and travelers 3
rooms = 1
travelers1 = 3

rooms 2 and travelers 3
rooms = 2
travelers1 = 2
travelers2 = 1

rooms 3 travelers 5
rooms = 3
travelers1 = 2
travelers2 = 2
travelers3 = 1

rooms 5 and travelers 5
travelers1 = 1
travelers2 = 1
travelers3 = 1
travelers4 = 1
travelers5 = 1

TIA,
Victor

2. minimum number of people in a room:
minPerRoom = Math.floor(travelers / rooms)

This might leave you with some people not in a room. You can find out how many people remain via
remainder = travelers % minPerRoom

Fill an array of size rooms with the value minPerRoom
Then, loop over the array, incrementing the first remainder elements by 1.

3. Sorry, it should be
remainder = travelers - (minPerRoom * rooms)

4. You could also use the rounded up value instead, and then use 1 less than that when the remaining travellers divided by the remaining rooms equals that 1 less value.

5. Code:
```function travellersForRoom(rooms, travellers, room) {
var basePerRoom = Math.floor(travellers / rooms);
var extraPerRoom = room <= travellers &#37; rooms ? 1 : 0;
return basePerRoom + extraPerRoom;
}```
For example, with 3 rooms and 8 travellers, how many in room 2?
Code:
`var room2 = travellersForRoom(3, 8, 2);`

6. Originally Posted by victorcorey
I have 2 values travelers and rooms. I need to calculate the people per room. So if rooms = 1 then travelers = travelers. But if rooms is greater than 1, I need to distribute the travelers evenly across the rooms.

Example
rooms 1 and travelers 3
rooms = 1
travelers1 = 3

rooms 2 and travelers 3
rooms = 2
travelers1 = 2
travelers2 = 1

rooms 3 travelers 5
rooms = 3
travelers1 = 2
travelers2 = 2
travelers3 = 1

rooms 5 and travelers 5
travelers1 = 1
travelers2 = 1
travelers3 = 1
travelers4 = 1
travelers5 = 1

TIA,
Victor
How about using an array?

Code javascript:
```for (var i = 0; i < travelers; i++) {
travelers[i % rooms] += 1;
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•