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

234
Vistas
Variable not updating through a nested for/if statement Javascript (beginner)

Problem with expected solutions can be found here : https://www.codewars.com/kata/58287977ef8d4451f90001a0/train/javascript

I have played around with console results and can see that whatever I set as my variable in the 2nd line of code let allSame = true; is what the end result returns. As I understand the for loop HAS to play out and so the IF statement then MUST play out - So one way or the other the variables should be updated. Maybe I have missed something out when it comes to global/local variables?

Please include an explanation with answer as I am trying to learn -- Thanks!


function isSameLanguage(list) {
  let allSame = true;
  for(let i = 0; i<list.length-2; i++)
    if(list[i]["language"] == list[i+1]["language"]){
      let allSame = true;      
    }else{
      let allSame = false;
    }
  return allSame;
  
  
}

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

function isSameLanguage(list) {
  var allSame = true;
  for(let i = 0; i<list.length-2; i++)
    if(list[i]["language"] == list[i+1]["language"]){
      allSame = true;      
    }else{
      allSame = false;
    }
  return allSame;
 
}

Global decclartion of allsame variable is required. if you create in between each if ladder is will become local scope.

about 4 years ago · Juan Pablo Isaza Denunciar
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