DEV Community

Cesar Del rio
Cesar Del rio

Posted on • Edited on

3 3

#5 - People in the Bus CodeWars Kata (6 kyu)

Instructions:
There is a bus moving in the city, and it takes and drop some people in each bus stop.

You are provided with a list (or array) of integer pairs. Elements of each pair represent number of people get into bus (The first item) and number of people get off the bus (The second item) in a bus stop.

Your task is to return number of people who are still in the bus after the last bus station (after the last array). 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

Take a look on the test cases.

Please keep in mind that the test cases ensure that the number of people in the bus is always >= 0. So the return integer can't be negative.

The second value in the first integer array is 0, since the bus is empty in the first bus stop.

My solution:

var number = function(busStops){
  let totalEntries = 0
  let totalDowns = 0

  busStops.map((arr)=> { 
    totalEntries += arr[0] 
    totalDowns += arr[1]
  })

  return totalEntries  - totalDowns

}
Enter fullscreen mode Exit fullscreen mode

Explanation
I started with an accumulator for the total entries and the total of persons that get off the bus, then I mapped the busStops array, I added the first value of every array to the total entries accumulator and the second value to the total downs accumulator, then I returned the total entries value minus the totalDowns value, so I get the people left in the bus

My Github
My twitter
Solve this Kata

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay