Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

adding and subtracting in a 'for' loop

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

I would go through the array with a counting index (like you do). Depending on this index, I would add or substract the value from the last result.

First Hint: Loop over an array with key-value pairs like this

for (const [key, value] of array) {console.log(key, value);}

Second Hint: A subtraction is a addition with a negativ number

5-3 == 5+(3*-1)

Third Hint: Have a look at the modul Operator %

console.log(1%2);
console.log(2%2);
console.log(3%2);

Solution:

var number = function (busStops) {

var total = 0;

for (const [i, value] of busStops){

total += value*(i%2?1:-1);

}

return total;

}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading