0

console.log changes function output value

I thought console.log doesn't change an output for function. Can someone explain why does this heppen?

function S(e) {
    for (var t = '', n = e.charCodeAt(0), i = 1; i < e.length; ++i)
            console.log(i, n ); 
        t += String.fromCharCode(e.charCodeAt(i) ^ ((i + n) & 127));
 
    return t;
}

S(":h_OWO4a'16*4g.&8kn") // return 'M'

and without using console.log completly different result

function S(e) {
    for (var t = '', n = e.charCodeAt(0), i = 1; i < e.length; ++i)
        t += String.fromCharCode(e.charCodeAt(i) ^ ((i + n) & 127));
 
    return t;
}

S(":h_OWO4a'16*4g.&8kn") // return 'Script error for "'
  • 5
    Missing braces in your for loop. – Roger Lipscombe May 22 '23 at 09:33
  • 1
    In your case it is missing braces, but there are plenty of ways that that calling `console.log` could change output depending on what you pass to it. (ie. mistyped equality checking `console.log(i = 0);` or incrementing in the log `console.log(i++);` etc.) – pilchard May 22 '23 at 09:39

0 Answers0