I’m back with a new issue!
I missed the previous issues because I was traveling around the country and couldn’t access my laptop. Although I wanted to schedule it in advance, I didn’t get time.
Anyway, let’s dive into today’s issue and the JavaScript problem-solving challenge.
I’ve also made a small change to this newsletter: instead of three JavaScript interview questions, I’ll provide just one question so we can focus on a single concept and understand it better.
In case you missed the previous issue
JavaScript Practice with Ecommerce Book Store Dataset
Last week, I shared a ChatGPT prompt that helps React developers turn HTML into reusable React components effortlessly.
Today’s challenge is very interesting and took me some time to create.
JavaScript Elevator Puzzle
Problem Statement
You need to simulate the behavior of an elevator in an office building with 4 floors (ground to floor 4). The elevator has a maximum capacity of 6 passengers at any time.
You are given an object with data on the passengers waiting to board and exit on each floor.
For each floor:
waitingPeople
indicates the number of people waiting to board the elevator.exitPeople
indicates the number of people who will exit the elevator on that floor.
Your task is to write a function that will return an array of floors where the elevator will stop.
The elevator only stops at a floor if there are people waiting to board or if there are passengers needing to exit.
Function Signature
/**
* Determines the floors where the elevator will stop.
* @param {Object} passengers - An object containing passenger data for each floor.
* @returns {string[]} An array of floor names where the elevator will stop.
*/
function elevatorStops(passengers) {
// implementation here
}
Test Cases
// Test Case 1
const passengers1 = {
ground: {
waitingPeople: 6
},
floor1: {
waitingPeople: 2,
exitPeople: 3
},
floor2: {
waitingPeople: 2,
exitPeople: 0
},
floor3: {
waitingPeople: 2,
exitPeople: 0
},
floor4: {
waitingPeople: 2,
exitPeople: 6
},
};
const result1 = elevatorStops(passengers1)
console.log(result1)
// Expected output: ['ground', 'floor1', 'floor2', 'floor4']
// Test Case 2
const passengers2 = {
ground: {
waitingPeople: 5
},
floor1: {
waitingPeople: 0,
exitPeople: 3
},
floor2: {
waitingPeople: 0,
exitPeople: 0
},
floor3: {
waitingPeople: 2,
exitPeople: 0
},
floor4: {
waitingPeople: 4,
exitPeople: 4
},
};
const result2 = elevatorStops(passengers2)
console.log(result2)
// Expected output: ['ground', 'floor1', 'floor3', 'floor4']
Hint (only read if you are stuck)
Your function should iterate over each floor in the passengers
object, check if there are waitingPeople
or exitPeople
, and add the floor to the result if either condition is met.
JavaScript Interview Question
What will be the output of the following code snippet?
let num = 0
let i = 5
while (i > 0) {
if (i % 2 !== 0) {
num += i
}
i--
}
console.log(num)
A) 0
B) 5
C) 9
D) 12
Thank you for reading this issue!
If you enjoyed the challenge and want more coding exercises, feel free to check out previous issues.
You can also do two more things to favor my content:
Don’t forget to subscribe for future newsletters.
Share it with others who might find it useful.