0

please advise why this function logs 'undefined'?

var intNumbers = [-1,2,-3,4,-5,6,-7,0,-8];

var signCount = function(array) {
  var result = 0;
  for (let i = 0; i < array.length; ++i) {
    if (Math.sign(array[i]) !== 1 && array[i] !== 0) {
      ++result;
    }
  }
  return result;
};

console.log(signCount(intNumbers)); // 5 undefined

I understand why it logs 5 but I've no idea where 'undefined' comes from...

Sigma
  • 234
  • 2
  • 6
  • 4
    are you running it in the console? – Yousaf Jul 06 '21 at 08:05
  • Does this help: https://stackoverflow.com/questions/14633968/chrome-firefox-console-log-always-appends-a-line-saying-undefined – Salman A Jul 06 '21 at 08:07
  • Why so complicated? Instead of testing the sign and being not equal to zero, you could just test `array[i] > 0` – Ronald Jul 06 '21 at 08:08
  • Only pasting this into the console will log the `undefined`. Replace `console.log(signCount(intNumbers));` with `signCount(intNumbers);` to prevent that –  Jul 06 '21 at 08:08
  • The same reason when you do `console.log('hello')` you get `hello` , `<- undefined`,.. `console.log()` returns undefined. And if you did `5 * 10` you would get `<- 50` in the console. (My preffered calculator, ctrl+shift+I).. – Keith Jul 06 '21 at 08:10

0 Answers0