I'd like to start by saying that I am still new to JavaScript and this is a CodeWars Kata Number of people in the Bus.
Also I know there is a simpler way of completing this task but If I just googled the answer, I feel that I wont have learned anything so here goes:
Hi All, at the end of a loop, how do I get it to add a, minus b on repeat? what is that called?
i.e.
[ 10, 0, 3, 5, 5, 8 ]
should work math like:
[ 10 - 0 + 3 - 5 + 5 - 8 ]
it's a codewars kata and I know there is a simple way of doing it but I have gone around it the long way.
Here is the code I am up to (and the console.log that is the test case)
var number = function (busStops) {
let newBusStops = [];
for (let i = 0; i < busStops.length; i++) {
newBusStops = newBusStops.concat(busStops[i]);
}
// return newBusStops;
let passengers = 0;
for (let i = 0; i < newBusStops.length; i++) {
passengers += newBusStops[i];
}
return passengers;
};
// var number = function (busStops) {
// let passengers = 0;
// for (let i = 0; i < busStops.length; i++) {
// passengers += parseInt(number[i]);
// busStops.toString();
// return busStops;
// }
// };
// var number = function (busStops) {
// for (let i = 0; i < busStops.length; i++) {
// return busStops[i][0] - busStops[i][1];
// }
// };
// return busStops[0][0];
console.log(
number([
[10, 0],
[3, 5],
[5, 8],
])
);
console.log(
number([
[3, 0],
[9, 1],
[4, 10],
[12, 2],
[6, 1],
[7, 10],
])
);
I've managed to flatten the 2d array but I am only able to add the flattened array, I can't figure out how to do add a minus b
. I don't know what that is called so I can search it