1

Alright, I have a function

function foo(input) {
    input = input.split("\n");
    console.log(input);
}

and the input is 'alert("gsasdgasdg")' so foo('alert("gsasdgasdg")') the output I get when I run it in nodejs is the expected output: [ 'alert("gsasdgasdg")' ] ` Now I will add a simple loop,

function foo(input) {
    var output = "";
    input = input.split("\n");
    console.log(input);
    for (var i in input) {
        console.log(input[i]);
    }
}

Now I get the first console.log output as [ 'alert("gsasdgasdg")' ] (expected) but there should be only one console.log when it loops through the array, what I get is alert("gsasdgasdg") (expected) and then [Function] as far as I know nodejs should have not of converted this string into a function, any insight that explains why this happens?

kumardeepakr3
  • 395
  • 6
  • 16
  • The reason you get Array brackets for the first one and not the second, is because in the first one `input` is an array when `input[i]` is the value for the index `i` and therefor not an array. – GillesC Dec 11 '16 at 12:55
  • @GillesC That was not the question, what I'm trying to ask is why input[1] is a function. –  Dec 11 '16 at 12:56
  • Did you check it was actually a function? To see if it's the logging that is wrong or if it is node actually changing a string to a function. – GillesC Dec 11 '16 at 13:00
  • ` try { input[i]() } catch (e) { console.log(e) }` did a quick test and this doesn't throw anything for input[1] –  Dec 11 '16 at 13:02
  • 1
    There's something in your code that attaches itself to `Array.prototype` – georg Dec 11 '16 at 13:04
  • There are modifications to the string prototype (rot13) but thats it –  Dec 11 '16 at 13:06
  • @DrGoat: try `console.log(i, input[i])`... – georg Dec 11 '16 at 13:20
  • turns out it was going through the prototype as well –  Dec 11 '16 at 13:28

0 Answers0