Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

246
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda