Learn Form Validation by Building a Calorie Counter - Step 39

Can any one let me know what’s wrong with my code? Thank you.

Step 39

JavaScript has a feature called template literals, which allow you to interpolate variables directly within a string. Template literals are denoted with backticks ````, as opposed to single or double quotes. Variables can be passed in to a template literal by surrounding the variable with ${} – the value of the variable will be inserted into the string.

Replace your concatenated string in the querySelector with a template literal – be sure to keep the space between your targetId variable and .input-container .

const calorieCounter = document.getElementById('calorie-counter');

const budgetNumberInput = document.getElementById('budget');

const entryDropdown = document.getElementById('entry-dropdown');

const addEntryButton = document.getElementById('add-entry');

const clearButton = document.getElementById('clear');

const output = document.getElementById('output');

let isError = false;

function cleanInputString(str) {

const regex = /[+-\s]/g;

return str.replace(regex, '');

}

function isInvalidInput(str) {

const regex = /\d+e\d+/i;

return str.match(regex);

}

function addEntry() {

const targetId = '#' + entryDropdown.value;

const templateLiteral = `${targetId}+ .input-container`;

const targetInputContainer = document.querySelector(templateLiteral);

}

Your browser information:

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

Challenge Information:

Learn Form Validation by Building a Calorie Counter - Step 39

You need to remove the + it is no longer needed and will just become part of the string which you do not want.

`${variable} .someClassName`

Thanks for response, i had tried in below way

function addEntry() {
  const targetId = '#' + entryDropdown.value;
  const templateLiteral = `${targetId} .input-container`;
  const targetInputContainer = document.querySelector(templateLiteral);
}

the error was

You should use a template literal in your querySelector method.

You added extra code. The template literal string should be directly inside the parentheses of querySelector()

Mod edit: solution code removed

@Zan1

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.