0

So, very new to learning javascript code and one of the problems I'm having with it is that it isn't allowing me to use the ${} in the line that is supposed to say console.log('For Loop Number: ${1}');

const todos = [
    {
        id: 1,
        text: 'Take out the trash',
        isCompleted: true 
    },
    {
        id: 2,
        text: 'Meeting with boss',
        isCompleted: true 
    },
    {
        id: 3,
        text: 'Dentist appt',
        isCompleted: true 
    }
];

// For
for(let i = 0; i < 10; i++) {
    console.log('For Loop Number: ${1}');
}

Now what it is supposed to be showing is a list of the numbers with For Loop in the console but I am not getting that. VERY new to javascript so keep that in mind, please.

  • 2
    Do you want ```console.log(`For Loop Number: ${i}`);```? Read about [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals). But this is a `ES6` feature, alternatively you can do `console.log("For Loop Number: " + i);` – Shidersz May 13 '19 at 05:52
  • 1
    You are using single quotes – Krishna Prashatt May 13 '19 at 05:52
  • 1
    instead of 1 do you need to print 'i' and replace ' with ` – HD.. May 13 '19 at 05:58
  • 1
    use the back tick symbol found in the top left corner of many keyboards right under the escape key. Use the back tick instead of quotation marks. – YulePale May 13 '19 at 05:59

4 Answers4

2

When it comes to template literals, you are supposed to pass in the variable/property as the expression. In addition, you are supposed to use back ticks(`), instead of the standard single or double quotes.

for(let i = 0; i < 10; i++) {
  console.log(`For Loop Number: ${i}`);
}

As you can see, your code will work fine after making the relevant changes.

const todos = [
  {
    id: 1,
    text: 'Take out the trash',
    isCompleted: true
  },
  {
    id: 2,
    text: 'Meeting with boss',
    isCompleted: true
  },
  {
    id: 3,
    text: 'Dentist appt',
    isCompleted: true
  }
];

// For
for (let i = 0; i < 10; i++) {
  console.log(`For Loop Number: ${i}`);
}
wentjun
  • 40,384
  • 10
  • 95
  • 107
2

try using backticks instead of quotes and i instead of 1.

for(let i = 0; i < 10; i++) {
    console.log(`For Loop Number: ${i}`);
}
Raghav Kukreti
  • 552
  • 5
  • 18
  • While the answer provides a solution to the issue described in the question, to make it more useful to the readers please describe why it works this way and it doesn't work as it is in the question. Also put a link to the relevant documentation page. – axiac May 13 '19 at 06:02
2

You need to use ` back ticks symbol.

const todos = [
    {
        id: 1,
        text: 'Take out the trash',
        isCompleted: true 
    },
    {
        id: 2,
        text: 'Meeting with boss',
        isCompleted: true 
    },
    {
        id: 3,
        text: 'Dentist appt',
        isCompleted: true 
    }
];

// For
for(let i = 0; i < 10; i++) {
    console.log(`For Loop Number: ${i}`);
}
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59
0

change

for(let i = 0; i < 10; i++) {
    console.log('For Loop Number: ${1}');
}

to

for(let i = 0; i < 10; i++) {
    console.log(`For Loop Number: ${i}`);
}
  • 2
    While the answer provides a solution to the issue described in the question, to make it more useful to the readers please describe why it works this way and it doesn't work as it is in the question. Also put a link to the relevant documentation page. – axiac May 13 '19 at 06:03
  • That should be `${i}`, not `${1}`. – SNag May 13 '19 at 06:04
  • when you will use loop then your value will be store define let/const/var. if you want to read particular any one position then your console will be console.log(`For loop Number : ${i[1]}`) – Praveen Kumar Tripathi May 13 '19 at 06:15