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.
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.
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).