Variable declared in the Loop declaration

Tell us what’s happening:
Hi , can anyone point put please where references for this Solution Exercise Loop declaration are?

for (var reversedStr = “”, i = str.length - 1; i >= 0; i–)

Plus, the variable in the loop declaration is declared with “var”, why?

:face_with_head_bandage:
Your code so far


function reverseString(str) {
let reversStr = "";
for (let i = str.length - 1; i >= 0; i--) {
  reversStr = reversStr  + str[i];
}
return reversStr;
}

reverseString("hello");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36.

Challenge: Reverse a String

Link to the challenge:

function reverseString(str) {
  for (var reversedStr = "", i = str.length - 1; i >= 0; i--) {
    reversedStr += str[i];
  }
  return reversedStr;
}

Are you talking about this solution from the hint post?

Declaring the variable within the for loop is effectively the same as declaring it before the for loop, in this case.

var has function scope, whereas let has block scope. Using var allows the function to reassign the value of reversStr. Using let would mean only the for conditions could assign the value.

Hi,
Thank you for your answer, appreciated. But it’s not the use of var deprecated.?
I have failed a job interview because I used var , but maybe I am not understanding.
Thank you anyway.

You can still use var, it’s just not a best practice.

many challenges have been written before ES6 became the standard, so the first part of the curriculum is still mostly with var, and ES6 is introduced in the next section of the curriculum

This year we are seeing a new update of the curriculum, with version 7.0, and it will be totally modernized with current standards.

It’s just poorly written code in my opinion. There is no reason to have the reversedStr variable declared/assigned in the loop like that. It looks like old-school JavaScript, which would also explain why it is using var because it’s old code.

That seems a little extreme.

As long as you know what you are doing, can explain scope and hoisting, and give some examples of why using let/const is often better, you should not “fail” a job interview just for using var. At least they should have asked why you used it and give you a chance to show that you understand the differences.

1 Like

Hi, thank you for your explanation. I said job interview but it was not an interview, it was one of those test to do at home in three hours.
Anyway, yes, my statement was a bit extreme, but the using of var was stigmatised

Automated tests are binary in nature, it’s just right or wrong answers, there is little room for gray areas. The use of var is more a gray area.

It is hard to gain much insight into what a candidate really knows based on just binary right and wrong answers. I would much prefer a wrong answer with some great thought process behind it over a correct answer with no thought process behind it.

I didn’t mean your statement was extreme, I meant disqualifying a candidate solely for using var is extreme (not in an automated test though).