SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, it should be
    remainder = travelers - (minPerRoom * rooms)

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by victorcorey View Post
    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;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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