Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

273
Vistas
Why does my console return that certain variables are not defined when the code works?

//learning javascript on udemy //Here is my code (well the instructor's code but my exact replication:

const magicSquare = [ [ 2, 7, 6 ], [ 9, 5, 1 ], [ 4, 3, 8 ] ];

for (let i = 0; i < magicSquare.length; i++) {
    let row = magicSquare[i];
    let sum = 0;
    for (let j = 0; j < row.length; j++) {
        sum += row[j];
    }
    console.log(`${row} summed to ${sum}`);
}

//So before i had wrote the last line: console.log(${row} summed to ${sum}); i would type in "sum" or "row" into my console just to check to make sure that it was working properly, and in return the console god would return to me:

Uncaught ReferenceError: sum is not defined

//so each time i got undefined i thought i must be doing something wrong so i kept going back and looking at the instructor's code and then at mine, and i was getting super frustrated because i knew we had the exact same thing, so i just finally decided to finish the whole thing and see if it ran. Sure enough, as soon as i put the last line which is the template literal; at the bottom it worked! But still if i type in : row or sum (without quotations of course) into the console it returns undefined! How can something work and be undefined? It was a great gaslighting operation..

//Is it because these variables are located within the loop? If i define them outside the loop they show up in my console.

//also stackoverflow is telling me CTRL + K my code for formatting, not sure if im doing this correctly, new user, first question, no programming experience. Thank you!

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda