【JS】【codewars】Number of people in the bus

原题:Number of people in the bus

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 arrays (or tuples). Each integer array has two items which 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.

翻译:公共汽车上的人数。
城市里有一辆公交车在行驶,它在每个汽车站接送一些人。

将为您提供整数数组(或元组)的列表(或数组)。每个整数数组有两个项目,分别表示在公交车站上车的人数(第一个项目)和下车的人数(第二个项目)。

您的任务是返回在最后一个汽车站之后(在最后一个数组之后)仍在公交车中的人数。即使这是最后一站公交车,公交车也不是空的,车上还有一些人,他们很可能睡在那里:d。

请看一下测试用例。

请记住,测试用例确保公交车上的人数始终大于等于0。因此返回整数不能为负。

第一个整数数组中的第二个值是0,因为第一个公共汽车站中的公交车是空的。

复制代码
 1 var number = function(busStops){
 2     var upSum=0;
 3     var downSum=0;
 4     for(var i=0; i<busStops.length; i++){
 5       upSum+=busStops[i][0];
 6       downSum+=busStops[i][1];
 7     }   
 8   var last=upSum-downSum;
 9   return last;
10 }
复制代码

 

posted @   冯荣新  阅读(199)  评论(0编辑  收藏  举报
编辑推荐:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
点击右上角即可分享
微信分享提示