0

Just trying to write some small program for learning, and first time using a self-invoked function. Can not understand why the result value is returned (as I saw in the Chrome Sources Debugger), but not printed out in the console as expected.

Thanks in advance for the explanation.

function secondLowestAndHighest(numbers) {
  //an empty array for the result
  let result = new Array;
  //sorting out the array by ascending order with built-in method .sort()
  let lowToHigh = numbers.sort(function(a, b) {
    return a - b;
  });
  //reverse the array of numbers
  let highToLow = new Array;
  (function() {
    //self-invoked function, to call out automatically
    for (let i = lowToHigh.length - 1; i >= 0; i--) {
      highToLow.push(lowToHigh[i]);
    }
    return highToLow;
  })();

  result.push(lowToHigh[1], highToLow[1]);

  return result;
}

//secondLowestAndHighest([13, 105, 75, 1, 344, 6, 5]);
console.log(secondLowestAndHighest([13, 105, 75, 1, 344, 6, 5]))
Satpal
  • 132,252
  • 13
  • 159
  • 168
M.Milos
  • 11
  • 1
  • 2
  • 2
    It's printing for me – RICKY KUMAR Apr 24 '18 at 13:17
  • because you're using an iffe wrong you dont need one here as its already self invoked – Joe Warner Apr 24 '18 at 13:17
  • @JoeWarner: I don't see anything wrong with the IIFE use in the question, can you be more specific? It's not *necessary*, but it's not wrong, and the OP clearly said they were doing this to learn. – T.J. Crowder Apr 24 '18 at 13:18
  • 1
    @M.Milos - Just FWIW, although you sometimes hear them called "self-invoking," they *aren't* self-invoking. (A self-invoking function is a *recursive* function.) What you have there is an IIFE -- an inline-invoked (or immediately-invoked) function expression. – T.J. Crowder Apr 24 '18 at 13:19
  • theres nothing wrong with the iffe but as OP states " first time using a self-invoked function. Can not understand why the result value is returned" i take that as meaning i thought "highToLow" would be returned but not sure what OP is trying to do – Joe Warner Apr 24 '18 at 13:19
  • Re the `undefined`: If you copy and paste this code into the console on most browsers, you'll see `undefined` **after** the correct output, because the return value of `console.log` is `undefined`. That's the only reason I can see why you would be getting `undefined` from the above. – T.J. Crowder Apr 24 '18 at 13:20
  • 1
    `return highToLow;` has no effect because you aren't using the return value, not even assigning it to anything. The code is no different from replacing the entire IIFE with just the for loop. –  Apr 24 '18 at 13:22
  • @ChrisG this was my thinking agreed – Joe Warner Apr 24 '18 at 13:22
  • @OP: you cannot return more than one thing from a function; the return inside the IIFE applies to the IIFE, not your `secondLowestAndHighest()` An IIFE is all about scope, it seems pretty useless here. –  Apr 24 '18 at 13:24
  • So, if I am getting you guys right, the only problem for not showing the result value in the console is that I did not specifically instruct it with the console.log(result)? Seems strange, as I thought the "return" keword always show something to the console. – M.Milos Apr 24 '18 at 13:27
  • @ChrisG, got it, so it is better practice to use a regular function for this case, so the returned value should be displayed then. Hopefully – M.Milos Apr 24 '18 at 13:30
  • @M.Milos This is a common beginner's mistake: `return` does not show anything in the console. `console.log()` prints to the console, `return` merely returns a value. This answer applies to python but should illustrate the difference: https://stackoverflow.com/a/7664904/5734311 –  Apr 24 '18 at 13:45

0 Answers0