skip to Main Content

I have a job assignment with a problem like this

There is a building with unlimited floors. Within each floor there are a number of lockers with consecutive locker numbers, in a configuration like this:
Floor. 1 there are 5 lockers, number 1-5
Floor. 2 there are 6 lockers, numbers 6-11
Floor. 3 there are 7 lockers, numbers 12-18
Floor. 4 there are 5 lockers, numbers 19-23
Floor. 5 there are 6 lockers, numbers 24-29
Floor. 6 there are 7 lockers, numbers 30-36
Etc…
Create a function that takes a locker number parameter, and returns the floor number

What’s the best code to solve this question? Thank you very much.

I did this code:

const Floor= 10;

for (let i = 1; i < lantai + 1; i++) {
    var lokerStart = 0;
    var lokerEnd = 0;
    
  if (i % 3 === 1) {
        lokerStart = i;
        lokerEnd = lokerStart + 4;
  } else if (i % 3 === 2) {
    lokerStart = 6;
        lokerEnd = lokerStart + 5;
  } else if (i % 3 === 0) {
    lokerStart = 12;
        lokerEnd = lokerStart + 6;
  }
  console.log("Floor", i, ", Locker Number", `${lokerStart} - ${lokerEnd}`);
}

but it resulted in:

Floor 1 ,Locker Number 1 – 5
Floor 2 ,Locker Number 6 – 11
Floor 3 ,Locker Number 12 – 18
Floor 4 ,Locker Number 4 – 8
Floor 5 ,Locker Number 6 – 11
Floor 6 ,Locker Number 12 – 18
Floor 7 ,Locker Number 7 – 11
Floor 8 ,Locker Number 6 – 11
Floor 9 ,Locker Number 12 – 18
Floor 10 ,Locker Number 10 – 14

I want a function to get a correct floor by inputing locker number 19 for example and get floor 4.

3

Answers


  1. A few things, firstly, you are resetting your start and end every loop. This is what causes it to drop the values every 3 floors. Secondly, you hardcoded the start values for i%3==2 and i%3==0. This makes it so that your values are always starting from there.

    Shift the initialising values outside and use the variables to determine the next start.

    const lantai= 10;
    var lokerStart = 0;
    var lokerEnd = 0;
    for (let i = 1; i < lantai + 1; i++) {
        
        
      if (i % 3 === 1) {
            lokerStart = lokerEnd + 1;
            lokerEnd = lokerStart + 4;
      } else if (i % 3 === 2) {
        lokerStart = lokerEnd + 1;
            lokerEnd = lokerStart + 5;
      } else if (i % 3 === 0) {
        lokerStart = lokerEnd + 1;
            lokerEnd = lokerStart + 6;
      }
      console.log("Floor", i, ", Locker Number", `${lokerStart} - ${lokerEnd}`);
    }
    

    Outputs:

    Floor 1 , Locker Number 1 - 5
    Floor 2 , Locker Number 6 - 11
    Floor 3 , Locker Number 12 - 18
    Floor 4 , Locker Number 19 - 23
    Floor 5 , Locker Number 24 - 29
    Floor 6 , Locker Number 30 - 36
    Floor 7 , Locker Number 37 - 41
    Floor 8 , Locker Number 42 - 47
    Floor 9 , Locker Number 48 - 54
    Floor 10 , Locker Number 55 - 59
    
    Login or Signup to reply.
  2. Your question body (your attempted solution) doesn’t seem to match your problem: you need to create a function that gets the locker number as the input (argument) and returns the floor.

    There’s probably a pure mathematical way to do that. Meanwhile, my solution here is a bit more naive, it starts at the first floor and keeps adding lockers according to your rule (5, 6, 7, 5, 6, 7, 5…) as we go up the floors (check the while), until finding the correct floor:

    function getFloor(n) {
      let currentFloor = 1;
      let lockersArray = [5, 6, 7];
      let lockersSoFar = lockersArray[(currentFloor - 1) % 3];
      while (n > lockersSoFar) {
        currentFloor++;
        lockersSoFar += lockersArray[(currentFloor - 1) % 3];
      }
      return currentFloor;
    };
    
    console.log(getFloor(4))
    console.log(getFloor(18))
    console.log(getFloor(19))
    console.log(getFloor(42))
    console.log(getFloor(128))
    Login or Signup to reply.
  3. As noted by @GerardoFurtado, there is indeed a pure mathematical way to solve this problem (a terser version can be found in the snippet below):

    function findFloor(lockerNumber) {
      const quotient = Math.floor(lockerNumber / 18);
      const remainder = lockerNumber % 18;
      
      if (remainder > 11) {
        return quotient * 3 + 3;
      } else if (remainder > 5) {
        return quotient * 3 + 2;
      } else {
        return quotient * 3 + 1;
      }
    }
    

    Explanation

    First, let’s have a look at an ASCII visualization of the building in question:

     1 | # # # # #
     2 | # # # # # #
     3 | # # # # # # #
    
     4 | # # # # #
     5 | # # # # # #
     6 | # # # # # # #
    
     7 | # # # # #
     8 | # # # # # #
     9 | # # # # # # #
    
    10 | # # # # #
    11 | # # # # # #
    12 | # # # # # # #
    
    ...
    

    See how they repeat? Every 3 floors the number of locker goes from 5 through 7 and then it becomes 5 again. Since these blocks are the same, we just need to find how many of them are there. A simple division will do (remember to round it down to the nearest integer). Note that, since this is the number of blocks, we need to multiply it by 3 to get the number of corresponding floors.

    const quotient = Math.floor(lockerNumber / 18);
    

    Now, how to find the rest of them? You probably got it right: the remainder of the division above.

    const remainder = lockerNumber % 18;
    

    The important part: If the remainder is greater than 11, we have three floors left to climb. If it is greater than 5 but less than or equal to 11, two floors. Else, one floor left. Make sure to add them to our final result.

    if (remainder > 11) {
      return quotient * 3 + 3;
    } else if (remainder > 5) {
      return quotient * 3 + 2;
    } else {
      return quotient * 3 + 1;
    }
    

    Try it:

    function findFloor(lockerNumber) {
      const quotient = Math.floor(lockerNumber / 18);
      const remainder = lockerNumber % 18;
      
      return quotient * 3 + 1 + Math.floor(remainder / 6);
    }
    
    for (let i = 1; i < 1e9; i += Math.random() * 1e8 | 0) {
      console.log(i, findFloor(i));
    }
    .as-console-wrapper {
      max-height: 100% !important;
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search