There is a bus moving in the city, and it takes and drops some people in each bus stop.
You are provided with an array of "bus stops". Each "bus stop" is itself an array containing 2 items:
- the number of people who get on bus at that bus stop
- the number of people who get off the bus at that bus stop
For example, [2, 5]
means that 2 people got on the bus, and 5 people got off the bus at that stop.
Your task is to return number of people who are still in the bus after the last bus station. Even though it is the last bus stop, the bus is not empty and some people are still in the bus, and they are probably sleeping there :D
Example input and output:
numberOfPassengersLeft([ [10,0], [3,5], [5,8] ]) // returns 5
numberOfPassengersLeft([ [3,0], [9,1], [4,10], [12,2], [6,1], [7,10] ]) // returns 17
numberOfPassengersLeft([ [3,0], [9,1], [4,8], [12,2], [6,1], [7,8] ]) // returns 21
numberOfPassengersLeft([ [3,0], [3,9] ]) // returns 'invalid input!'
The number of people in the bus is always >= 0, so the return value can't be negative.
CodeWars page for this kata: https://www.codewars.com/kata/number-of-people-in-the-bus/train/javascript