Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!