Don't Use functions in a Loop? - Can someone provide some insight -

I solved Binary Agents - There was a suggestion from FCC in a yellow triangle - that I should not use a function in a loop… Can someone provide some insight?

function binaryAgent(str) {
 //  128  64  32  16  8  4  2  1
 var binary = [128, 64, 32, 16, 8, 4, 2, 1];
 var result = []; 
  str = str.split(" ");
  for (var j = 0; j < str.length; j++ ){
  str[j] = str[j].split("");
var code =, i){
    return str[j][i] * val; 
var number =  code.reduce(function(a, b){
    return a+b;
  result.push( (String.fromCharCode(number) ) );
return result.join("");

binaryAgent("01001001 00100000 01101100 01101111 01110110 01100101 00100000 01000110 01110010 01100101 01100101 01000011 01101111 01100100 01100101 01000011 01100001 01101101 01110000 00100001"); 

Hi there,

I hope you don’t mind that I went into your post and edited it to make the code more legible. Three back-ticks above and below your code will make it format properly on the forum.

The warning actually says to not make a function within a loop, and the importance is crucial. In short, when you define a function, the compiler has to do all sorts of stuff, but when you invoke a function, it’s basically just pointing to a location in memory that contains code to run. It’s not a big deal when you’re just running the code once, but imagine having to go through all the extra function creation ceremony 100,000 times or more and the work adds up. So, it’s best to not define functions inside of loops. If you define the function outside of the loop, and then use that function’s name in your call to map, the error will go away.


#1- Thanks for making code more legible …

#2 - Thank for the clarity… I appreciate you taking the time to respond in a concise and precise manner…

Hello, I have moved your post to the right category int he forum as this is not a wiki entry.