Skip to content

Commit 5f321ee

Browse files
committed
Number of the people in the bus
1 parent 0689f73 commit 5f321ee

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

NumberPeopleInBus.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
3+
//----Description----
4+
//There is a bus moving in the city which takes and drops some people at each bus stop.
5+
//
6+
// You are provided with a list (or array) of integer pairs. Elements of each pair represent the number of people that get on the bus (the first item) and the number of people that get off the bus (the second item) at a bus stop.
7+
//
8+
// Your task is to return the number of people who are still on the bus after the last bus stop (after the last array). Even though it is the last bus stop, the bus might not be empty and some people might still be inside the bus, they are probably sleeping there :D
9+
//
10+
// Take a look on the test cases.
11+
//
12+
// Please keep in mind that the test cases ensure that the number of people in the bus is always >= 0. So the returned integer can't be negative.
13+
//
14+
// The second value in the first pair in the array is 0, since the bus is empty in the first bus stop.
15+
16+
var number = function(busStops){
17+
let passengersLeft = 0
18+
19+
for (let index=0; index < busStops.length; index++) {
20+
passengersLeft = passengersLeft + busStops[index][0] - busStops[index][1]
21+
}
22+
return passengersLeft
23+
}

0 commit comments

Comments
 (0)