Problem Can we fit remaining passengers in the bus using JavaScript


Problem

We are required to write a JavaScript function that takes in three parameters −

  • cap − is the amount of people the bus can hold excluding the driver.

  • on − is the number of people on the bus excluding the driver.

  • wait − is the number of people waiting to get on to the bus excluding the driver.

If there is enough space, we should return 0, and if there isn't, we should return the number of passengers we can't take.

Example

Following is the code −

 Live Demo

const cap = 120;
const on = 80;
const wait = 65;
const findCapacity = (cap, on, wait) => {
   let x = 0;
   let y = (on + wait);
   if(y > cap){
      x += (y - cap);
   };
   if(x < 0){
      x = 0;
   };
   return x;
};
console.log(findCapacity(cap, on, wait));

Output

25

Updated on: 17-Apr-2021

202 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements