# Challenge Guide: Create Strings using Template Literals

**URL:** <https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [March 20, 2021, 7:57pm UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117 "2021-03-20T19:57:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![as002](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@as002](https://forum.freecodecamp.org/u/as002)\
**Post date:** [March 20, 2021, 7:57pm UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/1 "2021-03-20T19:57:13Z")

</div>

Good morning everyone!

I am having some trouble understanding the solution to the mentioned challenge.  
I had to check how to solve it but dont really understand how it was done and i do not wish to move on until I do:

> [@freeCodeCamp Challenge Guide: Create Strings using Template Literals](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-create-strings-using-template-literals/301200):
>
> [Create Strings Using Template Literals](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/create-strings-using-template-literals)Problem Explanation Instead of using string concatenation, ES6 offers template literals to create strings. In this challenge, you have to use template literals to create an array of text warnings. It’s required to use template literals to return a list as every element in the array as the element will be wrapped in a \<li\>\</li\> tag. Hints Hint 1 Iterate through each element of arr and add a new element consisting of a template literal using arr’s elemen…

My question is how does the arr push the 3 different options to the array list from the result const. I am having trouble understanding how it knows to get the text from result.failure  
I understand the placeholder, just not how the i gets its value.

(my apologies if it is a dumb question but I have been around this for 2 hours and it is really bugging me)

```auto
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) {
 
  const failureItems = [];
  for (let i = 0; i < arr.length; i++)
  {failureItems.push(`<li class="text-warning">${arr.[i]}</li>`);}

  return failureItems;
}

const failuresList = makeList(result.failure);

```

---

<div class="post-metadata">

**Author:** ![egglearn](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/egglearn/32/151803_2.png) [@egglearn](https://forum.freecodecamp.org/u/egglearn)\
**Post date:** [March 20, 2021, 8:24pm UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/2 "2021-03-20T20:24:34Z")

</div>

ok so the function named makesList has a parameter arr. When the function is called, the argument passed is result. failure. what is result.failure?

```auto
const result = {
  success: ["max-length", "no-amd", "prefer-arrow-functions"],
  failure: ["no-var", "var-on-top", "linebreak"],
  skipped: ["no-extra-semi", "no-dup-keys"]
};

```

its this array below, try console.log(result.failure)

```auto
["no-var", "var-on-top", "linebreak"]

```

so in the for loop, you are looping over arr which is actually result.failure. Therefore  
in the template literal, when you write

${arr[i]} for each iteration this will be one of the elements in arr ie one of the elements in result. failure, ie one of the elements in

```auto
["no-var", "var-on-top", "linebreak"]

```

make sense?

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [March 20, 2021, 8:43pm UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/3 "2021-03-20T20:43:28Z")

</div>

> [@as002](#):
>
> how it knows to get the text from result.failure

> [@as002](#):
>
> const failuresList = makeList(result.failure);

the function is called with `result.failure` as argument

---

<div class="post-metadata">

**Author:** ![as002](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@as002](https://forum.freecodecamp.org/u/as002)\
**Post date:** [March 21, 2021, 10:45pm UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/4 "2021-03-21T22:45:42Z")

</div>

Thank you very much! I see now what was missing in my line of thought, the const failuresList = makeList(result.failure).

Reading your explanation made the click! Thank you very much!

---

<div class="post-metadata">

**Author:** ![hustlework53](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hustlework53/32/151830_2.png) [@hustlework53](https://forum.freecodecamp.org/u/hustlework53)\
**Post date:** [April 11, 2021, 5:29pm UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/5 "2021-04-11T17:29:38Z")

</div>

@egglearn where does argument arr defined to equal result.failure?

---

<div class="post-metadata">

**Author:** ![hustlework53](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hustlework53/32/151830_2.png) [@hustlework53](https://forum.freecodecamp.org/u/hustlework53)\
**Post date:** [April 11, 2021, 5:31pm UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/6 "2021-04-11T17:31:46Z")

</div>

@egglearn Where does argument arr defined to equal result.failure?

---

<div class="post-metadata">

**Author:** ![hustlework53](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hustlework53/32/151830_2.png) [@hustlework53](https://forum.freecodecamp.org/u/hustlework53)\
**Post date:** [April 11, 2021, 5:36pm UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/7 "2021-04-11T17:36:39Z")

</div>

> [@egglearn](#):
>
> When the function is called, the argument passed is result. failure.

How? can you please explain me in detail, I’m really depressing. How does that argument arr knows result.failure? In which step it’s defined to equal?

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [April 11, 2021, 5:52pm UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/8 "2021-04-11T17:52:20Z")

</div>

> [@as002](#):
>
> const failuresList = makeList(result.failure);

this last line is where the function is called, and it has `result.failure` as argument, so when the function execute that is the value that `arr` takes

---

<div class="post-metadata">

**Author:** ![shagariboy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shagariboy/32/215701_2.png) [@shagariboy](https://forum.freecodecamp.org/u/shagariboy)\
**Post date:** [April 26, 2021, 7:38pm UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/9 "2021-04-26T19:38:16Z")

</div>

please what does this code mean:

```auto
for (let i = 0; i < arr.length; i++)

```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [April 26, 2021, 7:57pm UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/10 "2021-04-26T19:57:51Z")

</div>

That is a for loop using values of `i` from `0` to the length of the array `arr`.

---

<div class="post-metadata">

**Author:** ![xxh](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/xxh/32/135268_2.png) [@xxh](https://forum.freecodecamp.org/u/xxh)\
**Post date:** [May 7, 2021, 5:19am UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/11 "2021-05-07T05:19:43Z")

</div>

I did it slightly differently:

```auto
  const failureItems = [];
  let i = failureItems.length;
  for (i = 0; i < 3; i++) {
    failureItems.push(`<li class="text-warning">${result.failure[i]}</li>`)
    }

```

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [May 7, 2021, 5:22am UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/12 "2021-05-07T05:22:07Z")

</div>

Hi @xxh !

Welcome to the forum!

I have added spoiler tags around your code since it is a full working solution and we don’t want to spoil it for others that haven’t done this problem yet.

---

<div class="post-metadata">

**Author:** ![a\_aramini](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/a_aramini/32/218659_2.png) [@a\_aramini](https://forum.freecodecamp.org/u/a_aramini)\
**Post date:** [May 10, 2021, 11:50pm UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/13 "2021-05-10T23:50:55Z")

</div>

Hi @jwilkins.oboe !

The code in the 1st post also contains the full working solution. The OP posted the code because their initial question was about how/why it works.

Might want to put a spoiler alert on that as well?

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [May 11, 2021, 12:13am UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/14 "2021-05-11T00:13:48Z")

</div>

I just added the spoiler tags:grinning:

---

<div class="post-metadata">

**Author:** ![a\_aramini](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/a_aramini/32/218659_2.png) [@a\_aramini](https://forum.freecodecamp.org/u/a_aramini)\
**Post date:** [May 12, 2021, 10:27pm UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/16 "2021-05-12T22:27:09Z")

</div>

> [@xxh](#):
>
> I did it slightly differently:

Yes, it passes the test for the specific case of `result.failure`. But since you hard coded a reference to that in the string literal template inside the function, it wouldn’t work for all cases (`result.success` and `result.skipped`). What would the output be for the other cases? try:

```auto
console.log(makeList(result.success);

```

and

```auto
console.log(makeList(result.skipped);

```

See what I’m saying?

Also you hard coded a number for the number of loops to perform in the conditional part of the loop.

What if the arrays were longer than 3 items long?

How could you re-write it so that it would work even if you didn’t know the length of the array being passed to the function?

And the line above the for loop, it really isn’t doing anything useful.

Not picking on you, but for learning purposes, just challenging you to think about what’s happening in your code, and why what I’ve pointed out is the case . Maybe you can fix it to address these issues.

---

<div class="post-metadata">

**Author:** ![xxh](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/xxh/32/135268_2.png) [@xxh](https://forum.freecodecamp.org/u/xxh)\
**Post date:** [May 14, 2021, 12:21am UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/17 "2021-05-14T00:21:49Z")

</div>

Thanks for your post a\_aramini !  
I agree that hard-coding is rendering this code useless for the other two scenarios. I took a better look, and it looks like using the argument passed to `makeList` (both in `i < arr.length;` and `${arr[i]}` is a much better solution. And I agree about the line above (looks like it was a leftover from what I had coded while looking for a solution). I’m not posting the result (as it will be blurred 😀 ).  
Thanks again for challenging what I wrote earlier!

---

<div class="post-metadata">

**Author:** ![reedm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/reedm/32/220359_2.png) [@reedm](https://forum.freecodecamp.org/u/reedm)\
**Post date:** [June 7, 2021, 12:52am UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/18 "2021-06-07T00:52:31Z")

</div>

I used a `for...in` loop and got the desired output, but am not able to pass the challenge. Is there something wrong with doing it this way or is it just that this has not been added as an acceptable solution?

```auto
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 = [];
  for(obj in arr) {
  failureItems.push(`<li class="text-warning">${arr[obj]}</li>`);
  }
  // Only change code above this line
  return failureItems;
}
const failuresList = makeList(result.failure);

```

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [June 7, 2021, 1:11am UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/19 "2021-06-07T01:11:48Z")

</div>

You have to declare the `obj` variable in the [for…in](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) loop.

BTW, for arrays, you really should be using a [for…of](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) loop instead (`forEach` or `map` would be even better if you ask me). You can pass the challenge with just a map. The setup might make it look like you have to use the `failureItems` array, but you don’t.

```auto
function makeList(arr) {
  return arr.map(failureText => `<li class="text-warning">${failureText}</li>`);
}

```

---

<div class="post-metadata">

**Author:** ![Cangu](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cangu/32/221331_2.png) [@Cangu](https://forum.freecodecamp.org/u/Cangu)\
**Post date:** [June 14, 2021, 5:17pm UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/20 "2021-06-14T17:17:35Z")

</div>

Hi everyone, this is my first post.  
my code looks similar but doesnt work and I don´t know why.

```auto
  for (let i=0; i<arr.length; i++){
    failureItems.push(`<li class="text-warning"> ${arr[i]} </li>`);
  }

```

If log in the console the output:  
[ ‘

- no-var 
’,  
‘- var-on-top 
’,  
‘- linebreak 
’ ]

I´m failing because

> `failuresList` should be equal to the specified output.

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [June 14, 2021, 5:20pm UTC](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117/21 "2021-06-14T17:20:17Z")

</div>

Closed on June 14, 2021, 5:20pm UTC



[Next page](https://forum.freecodecamp.org/t/challenge-guide-create-strings-using-template-literals/452117.md?page=2)
