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
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
andi%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.
Outputs:
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 thewhile
), until finding the correct floor: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):
Explanation
First, let’s have a look at an ASCII visualization of the building in question:
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.
Now, how to find the rest of them? You probably got it right: the remainder of the division above.
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.
Try it: