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

Why do I get an 'undefined' item after Array.reduce()?

I am trying to learn JavaScript. Now, I am doing experiments with arrays, JSON objects and the sort and reduce methods. The experiment is to find a nice solution to Codility.com’s NumberOfDiscIntersections task. I have seen the solution around, and I am trying to reproduce mine, so that I actually get the method.

My current code is almost done. What I ask here is: why am I getting a last ‘undefined’ element in the output of the execution of the following code?

This is the exact JavaScript code:

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

C = [2, 1, 1, 3, 2, 3];
answer = solution(C);
console.log(answer);

//circle radius varies from 0..2147483647
function solution(A) {
  //marker = {spot:0, openers:0, closers:0};
  const N = A.length;
  const points = [];
  let left = 0;
  let right = 0;

  // First, I count all borders
  for (i = 0; i < N; i++) {
    left = i - A[i];
    right = i + A[i];
    points.push({
      "position": left,
      "openers": 1,
      "closers": 0
    });
    points.push({
      "position": right,
      "openers": 0,
      "closers": 1
    });
  }
  console.log("points before sort:")
  for (item of points) {
    console.log(item);
  }
  points.sort((a, b) => {
    if (a.position < b.position) {
      return -1;
    } else if (a.position > b.position) {
      return 1;
    }
    return 0;
  });
  console.log("points after sort:")
  for (item of points) {
    console.log(item);
  }
  let parseable = points.reduce((acc, object) => {
    var index = acc.map((o) => o.position).indexOf(object.position);
    if (index == -1) {
      acc.push(object);
    } else {
      acc[index].openers += object.openers;
      acc[index].closers += object.closers;
    }
    return acc;
  }, []);
  console.log("points after reduce:")
  for (item of parseable) {
    console.log(item);
  }
}

Thia is my console output:

points before sort:
position: -2, openers: 1, closers: 0 }
position: 2, openers: 0, closers: 1 }
position: 0, openers: 1, closers: 0 }
position: 2, openers: 0, closers: 1 }
position: 1, openers: 1, closers: 0 }
position: 3, openers: 0, closers: 1 }
position: 0, openers: 1, closers: 0 }
position: 6, openers: 0, closers: 1 }
position: 2, openers: 1, closers: 0 }
position: 6, openers: 0, closers: 1 }
position: 2, openers: 1, closers: 0 }
position: 8, openers: 0, closers: 1 }
points after sort:
position: -2, openers: 1, closers: 0 }
position: 0, openers: 1, closers: 0 }
position: 0, openers: 1, closers: 0 }
position: 1, openers: 1, closers: 0 }
position: 2, openers: 0, closers: 1 }
position: 2, openers: 0, closers: 1 }
position: 2, openers: 1, closers: 0 }
position: 2, openers: 1, closers: 0 }
position: 3, openers: 0, closers: 1 }
position: 6, openers: 0, closers: 1 }
position: 6, openers: 0, closers: 1 }
position: 8, openers: 0, closers: 1 }
points after reduce:
position: -2, openers: 1, closers: 0 }
position: 0, openers: 2, closers: 0 }
position: 1, openers: 1, closers: 0 }
position: 2, openers: 2, closers: 2 }
position: 3, openers: 0, closers: 1 }
position: 6, openers: 0, closers: 2 }
position: 8, openers: 0, closers: 1 }
undefined

Why do I have this ‘undefined’ item in the end of the reduced array?

>Solution :

Because you are not returning anything from the solution function.

answer variable has default value undefined, which is the one getting logged in the console.

answer = solution(C); // `answer` is `undefined`
console.log(answer); // logs `undefined`

return something from the function, which answer variable can hold.

thanks

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