Why is resultDisplayArray used twice?

Tell us what’s happening:

I found it confusing that the variable (or is it called constant?)
const resultDisplayArray;
Was used twice.
Is there any specific reason why it was done like that? Is it necessary or best practice?

I found this code I first wrote easier to understand, and it gives the same outcome (but I did have to touch outside the area restricted in the instructions and the lesson will not accept it as valid).

const result = {
  success: ["max-length", "no-amd", "prefer-arrow-functions"],
  failure: ["no-var", "var-on-top", "linebreak"],
  skipped: ["id-blacklist", "no-dup-keys"]
};
function makeList(arr) {
  "use strict";
  
  // Only change code below this line
  const resultDisplayArray = [];
  for (let i = 0; i < arr.length; i++) {
    resultDisplayArray.push(`<li class="text-warning">${arr[i]}</li>`);
  }

  // Only change code above this line

  return resultDisplayArray;
}

const otherWord = makeList(result.failure);

console.log(otherWord);

This is the solution the lesson will accept:

Your code so far


const result = {
success: ["max-length", "no-amd", "prefer-arrow-functions"],
failure: ["no-var", "var-on-top", "linebreak"],
skipped: ["id-blacklist", "no-dup-keys"]
};
function makeList(arr) {
"use strict";

// Only change code below this line
const resultDisplayArray = [];
for (let i = 0; i < arr.length; i++) {
  resultDisplayArray.push(`<li class="text-warning">${arr[i]}</li>`);
}

// Only change code above this line

return resultDisplayArray;
}

const resultDisplayArray = makeList(result.failure);

console.log(resultDisplayArray);

Your browser information:

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

Challenge: Create Strings using Template Literals

Link to the challenge:

1 Like

My guess is that this same example is used elsewhere to demonstrate scoping of const and it has nothing to do with this particular lesson.
You could name that variable inside the function whatever you want, it would make no difference (if not for the lesson’s restrictions).

2 Likes

Yeah I agree. The use of the same variable twice does seem to add an unnecessary level of potential confusion.

1 Like

Yep, I am still confused about it. I thought a const variable could only be declared once, and here it’s declared twice (or maybe not because one time it’s inside a function?). See, I’m confused. Probably when I learn more this will seem obvious, but right now it isn’t yet.
Thanks for your reply!

I don’t know why the inner resultDisplayArray is used at all in the map example, I would suggest just returning the mapped array.

It’s just poor code if you ask me. The variable names and the reuse of an identifier in two separate scopes.

We should probably update the seed code with some better variable names. Maybe something like failures for the function scoped array and failuresList for the outer array. Didn’t really think too much about the names but that seems more logical. We should definitely not reuse the identifier.


Edit: I made a PR for this.