|
| 1 | +const calorieCounter = document.getElementById('calorie-counter'); |
| 2 | +const budgetNumberInput = document.getElementById('budget'); |
| 3 | +const entryDropdown = document.getElementById('entry-dropdown'); |
| 4 | +const addEntryButton = document.getElementById('add-entry'); |
| 5 | +const clearButton = document.getElementById('clear'); |
| 6 | +const output = document.getElementById('output'); |
| 7 | +let isError = false; |
| 8 | + |
| 9 | +function cleanInputString(str) { |
| 10 | + const regex = /[+-\s]/g; |
| 11 | + return str.replace(regex, ''); |
| 12 | +} |
| 13 | + |
| 14 | +function isInvalidInput(str) { |
| 15 | + const regex = /\d+e\d+/i; |
| 16 | + return str.match(regex); |
| 17 | +} |
| 18 | + |
| 19 | +function addEntry() { |
| 20 | + const targetInputContainer = document.querySelector(`#${entryDropdown.value} .input-container`); |
| 21 | + const entryNumber = targetInputContainer.querySelectorAll('input[type="text"]').length + 1; |
| 22 | + const HTMLString = ` |
| 23 | + <label for="${entryDropdown.value}-${entryNumber}-name">Entry ${entryNumber} Name</label> |
| 24 | + <input type="text" id="${entryDropdown.value}-${entryNumber}-name" placeholder="Name" /> |
| 25 | + <label for="${entryDropdown.value}-${entryNumber}-calories">Entry ${entryNumber} Calories</label> |
| 26 | + <input |
| 27 | + type="number" |
| 28 | + min="0" |
| 29 | + id="${entryDropdown.value}-${entryNumber}-calories" |
| 30 | + placeholder="Calories" |
| 31 | + />`; |
| 32 | + targetInputContainer.insertAdjacentHTML('beforeend', HTMLString); |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +function calculateCalories(e) { |
| 37 | + e.preventDefault(); |
| 38 | + isError = false; |
| 39 | + |
| 40 | + const breakfastNumberInputs = document.querySelectorAll("#breakfast input[type='number']"); |
| 41 | + const lunchNumberInputs = document.querySelectorAll("#lunch input[type='number']"); |
| 42 | + const dinnerNumberInputs = document.querySelectorAll("#dinner input[type='number']"); |
| 43 | + const snacksNumberInputs = document.querySelectorAll("#snacks input[type='number']"); |
| 44 | + const exerciseNumberInputs = document.querySelectorAll("#exercise input[type='number']"); |
| 45 | + |
| 46 | + const breakfastCalories = getCaloriesFromInputs(breakfastNumberInputs); |
| 47 | + const lunchCalories = getCaloriesFromInputs(lunchNumberInputs); |
| 48 | + const dinnerCalories = getCaloriesFromInputs(dinnerNumberInputs); |
| 49 | + const snacksCalories = getCaloriesFromInputs(snacksNumberInputs); |
| 50 | + const exerciseCalories = getCaloriesFromInputs(exerciseNumberInputs); |
| 51 | + const budgetCalories = getCaloriesFromInputs([budgetNumberInput]); |
| 52 | + |
| 53 | + if (isError) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + const consumedCalories = breakfastCalories + lunchCalories + dinnerCalories + snacksCalories; |
| 58 | + const remainingCalories = budgetCalories - consumedCalories + exerciseCalories; |
| 59 | + const surplusOrDeficit = remainingCalories < 0 ? 'Surplus' : 'Deficit'; |
| 60 | + output.innerHTML = ` |
| 61 | + <span class="${surplusOrDeficit.toLowerCase()}">${Math.abs(remainingCalories)} Calorie ${surplusOrDeficit}</span> |
| 62 | + <hr> |
| 63 | + <p>${budgetCalories} Calories Budgeted</p> |
| 64 | + <p>${consumedCalories} Calories Consumed</p> |
| 65 | + <p>${exerciseCalories} Calories Burned</p> |
| 66 | + `; |
| 67 | + output.classList.remove("hide");3 |
| 68 | +} |
| 69 | + |
| 70 | +function getCaloriesFromInputs(list) { |
| 71 | + let calories = 0; |
| 72 | + |
| 73 | + for (const item of list) { |
| 74 | + const currVal = cleanInputString(item.value); |
| 75 | + const invalidInputMatch = isInvalidInput(currVal); |
| 76 | + |
| 77 | + if (invalidInputMatch) { |
| 78 | + alert(`Invalid Input: ${invalidInputMatch[0]}`); |
| 79 | + isError = true; |
| 80 | + return null; |
| 81 | + } |
| 82 | + calories += Number(currVal); |
| 83 | + } |
| 84 | + return calories; |
| 85 | +} |
| 86 | + |
| 87 | +function clearForm() {}; |
| 88 | + |
| 89 | +addEntryButton.addEventListener("click", addEntry); |
| 90 | +calorieCounter.addEventListener("submit", calculateCalories); |
| 91 | + |
0 commit comments