Skip to content

Instantly share code, notes, and snippets.

@spinningarrow
Last active July 5, 2018 08:54
Show Gist options
  • Save spinningarrow/5bfc66c0da6757374a8d852ce8cd4338 to your computer and use it in GitHub Desktop.
Save spinningarrow/5bfc66c0da6757374a8d852ce8cd4338 to your computer and use it in GitHub Desktop.

Number of people in the bus

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:

  1. the number of people who get on bus at that bus stop
  2. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment