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.

4
  • 2
    Do you want console.log(`For Loop Number: ${i}`);? Read about template literals. But this is a ES6 feature, alternatively you can do console.log("For Loop Number: " + i); Commented May 13, 2019 at 5:52
  • 1
    You are using single quotes Commented May 13, 2019 at 5:52
  • 1
    instead of 1 do you need to print 'i' and replace ' with ` Commented May 13, 2019 at 5: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. Commented May 13, 2019 at 5:59

4 Answers 4

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}`);
}

Sign up to request clarification or add additional context in comments.

Comments

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}`);
}

1 Comment

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.
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}`);
}

3 Comments

FYI, ` is the backtick (or grave), ~ is tilde
sorry missed it!
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.
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}`);
}

3 Comments

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.
That should be ${i}, not ${1}.
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]})

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.