Create Strings using Template Literals

hi
i am trying to solve Create Strings using Template Literals

my code ;

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

  return failureItems;
}

const failuresList = makeList(result.failure);
console.log(failuresList)

it does work on vscode but on freecodecamp it doesn’t accept my code. But if i add
let a = “”
let i = 0; before “for”
like this

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

  return failureItems;
}

const failuresList = makeList(result.failure);

it does work. my question is what’s happening? i thought inside for loop "i "and "a " is local scoped so i don’t need to define them.

why does freecodecamp does not accept my code, but vscode runs it without warnings or problems?

The FCC tests run in strict mode, so you can’t just use a variable like that, you have to declare it first, which is what you are doing with let a = "".

Also, you can do away with a and b completely. You can create the template literal and pass it to push at the same time. And there is no reason to copy the arr into another variable. You are only accessing values from it, not changing it.

1 Like

And to expand on what bbsmooth said, with the way you created a and i using let, they are block scoped which means they are available in the current level they are on and below, including the for loop. Because of the way scoping works with let and const, a and i here are scoped to be available to the whole function since the next closest parent block (curly braces) is the function itself.

If you replace these line:

  let a = ""
  let i = 0;

with these lines in the FCC editor (since it is running in JS strict mode), you will see an error because the variables are only available in that block, and you cannot make a new variable without using a keyword to specify its scope.

//produces an error in "use strict" mode
 {
   let a = ""
   let i = 0;
 }
1 Like

I’m probably just misunderstanding you but an {} is a code block, you can use it to block scope variables. It works just fine in strict mode.

'use strict'
{
  // block scope
  let a = 1;
  console.log(a); // 1
}

console.log(a); // ReferenceError: a is not defined

You don’t see it used all that often. But if you need a temporary scope (for whatever reason) and you do not want to create a function just for it you can use a code block.

1 Like

My response was based on @expenjoyer’s code. It is meant to be inlaid inside of the other code, as I previously stated.

1 Like