curriculum/challenges/english/blocks/learn-form-validation-by-building-a-calorie-counter/63c2187f55eb0f400269568f.md
Finally, on a new line after your second label, create another input element. Give this one a type attribute set to number, a min attribute set to 0 (to ensure negative calories cannot be added), a placeholder attribute set to Calories, and an id attribute that matches the for attribute of your second label element.
You should have two input elements in your HTMLString.
const HTMLstring = code.split(/HTMLString\s*=/)[1];
assert.equal(HTMLstring.match(/<input/g).length, 2);
Your new input element should be on a new line.
const HTMLstring = code.split(/HTMLString\s*=/)[1];
assert.equal(HTMLstring.match(/\n\s*<input/g).length, 2);
Your new input element should come after your second label element.
const HTMLstring = code.split(/HTMLString\s*=/)[1];
const inputIndex = HTMLstring.lastIndexOf("<input");
const labelIndex = HTMLstring.lastIndexOf("<label");
assert.isAbove(inputIndex, labelIndex);
Your new input element should have a type attribute set to number.
const HTMLstring = code.split(/HTMLString\s*=/)[1];
const inputAttributes = HTMLstring.match(/<input\s+[^>]*>/g)[1];
assert.match(inputAttributes, /type\s*=\s*"number"/);
Your input element should have a placeholder attribute set to Calories.
const HTMLstring = code.split(/HTMLString\s*=/)[1];
const inputAttributes = HTMLstring.match(/<input\s+[^>]*>/g)[1];
assert.match(inputAttributes, /placeholder\s*=\s*"Calories"/);
Your input element should have an id attribute set to ${entryDropdown.value}-${entryNumber}-calories.
const HTMLstring = code.split(/HTMLString\s*=/)[1];
const inputAttributes = HTMLstring.match(/<input\s+[^>]*>/g)[1];
assert.match(inputAttributes, /id\s*=\s*"\${entryDropdown.value}-\${entryNumber}-calories"/);
Your input element should have a min attribute set to 0.
const HTMLstring = code.split(/HTMLString\s*=/)[1];
const inputAttributes = HTMLstring.match(/<input\s+[^>]*>/g)[1];
assert.match(inputAttributes, /min\s*=\s*"0"/);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="styles.css" />
<title>Calorie Counter</title>
</head>
<body>
<main>
<h1>Calorie Counter</h1>
<div class="container">
<form id="calorie-counter">
<label for="budget">Budget</label>
<input
type="number"
min="0"
id="budget"
placeholder="Daily calorie budget"
required
/>
<fieldset id="breakfast">
<legend>Breakfast</legend>
<div class="input-container"></div>
</fieldset>
<fieldset id="lunch">
<legend>Lunch</legend>
<div class="input-container"></div>
</fieldset>
<fieldset id="dinner">
<legend>Dinner</legend>
<div class="input-container"></div>
</fieldset>
<fieldset id="snacks">
<legend>Snacks</legend>
<div class="input-container"></div>
</fieldset>
<fieldset id="exercise">
<legend>Exercise</legend>
<div class="input-container"></div>
</fieldset>
<div class="controls">
<span>
<label for="entry-dropdown">Add food or exercise:</label>
<select id="entry-dropdown" name="options">
<option value="breakfast" selected>Breakfast</option>
<option value="lunch">Lunch</option>
<option value="dinner">Dinner</option>
<option value="snacks">Snacks</option>
<option value="exercise">Exercise</option>
</select>
<button type="button" id="add-entry">Add Entry</button>
</span>
</div>
<div>
<button type="submit">
Calculate Remaining Calories
</button>
<button type="button" id="clear">Clear</button>
</div>
</form>
<div id="output" class="output hide"></div>
</div>
</main>
<script src="./script.js"></script>
</body>
</html>
:root {
--light-grey: #f5f6f7;
--dark-blue: #0a0a23;
--fcc-blue: #1b1b32;
--light-yellow: #fecc4c;
--dark-yellow: #feac32;
--light-pink: #ffadad;
--dark-red: #850000;
--light-green: #acd157;
}
body {
font-family: "Lato", Helvetica, Arial, sans-serif;
font-size: 18px;
background-color: var(--fcc-blue);
color: var(--light-grey);
}
h1 {
text-align: center;
}
.container {
width: 90%;
max-width: 680px;
}
h1,
.container,
.output {
margin: 20px auto;
}
label,
legend {
font-weight: bold;
}
.input-container {
display: flex;
flex-direction: column;
}
button {
cursor: pointer;
text-decoration: none;
background-color: var(--light-yellow);
border: 2px solid var(--dark-yellow);
}
button,
input,
select {
min-height: 24px;
color: var(--dark-blue);
}
fieldset,
label,
button,
input,
select {
margin-bottom: 10px;
}
.output {
border: 2px solid var(--light-grey);
padding: 10px;
text-align: center;
}
.hide {
display: none;
}
.output span {
font-weight: bold;
font-size: 1.2em;
}
.surplus {
color: var(--light-pink);
}
.deficit {
color: var(--light-green);
}
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);
}
--fcc-editable-region--
function addEntry() {
const targetInputContainer = document.querySelector(`#${entryDropdown.value} .input-container`);
const entryNumber = targetInputContainer.querySelectorAll('input[type="text"]').length;
const HTMLString = `
<label for="${entryDropdown.value}-${entryNumber}-name">Entry ${entryNumber} Name</label>
<input type="text" id="${entryDropdown.value}-${entryNumber}-name" placeholder="Name" />
<label for="${entryDropdown.value}-${entryNumber}-calories">Entry ${entryNumber} Calories</label>`;
}
--fcc-editable-region--