|
| 1 | +const addPatientButton = document.getElementById("addPatient"); |
| 2 | +const report = document.getElementById("report"); |
| 3 | +const btnSearch = document.getElementById("btnSearch"); |
| 4 | +const patients = []; |
| 5 | + |
| 6 | +// This function captures user-entered data from the HTML form elements: name, gender, age, and medical condition. It ensures that all fields have valid inputs. |
| 7 | +function addPatient() { |
| 8 | + const name = document.getElementById("name").value; |
| 9 | + const gender = document.querySelector('input[name="gender"]:checked'); |
| 10 | + const age = document.getElementById("age").value; |
| 11 | + const condition = document.getElementById("condition").value; |
| 12 | + |
| 13 | + if (name && gender && age && condition) { |
| 14 | + patients.push({ name, gender: gender.value, age, condition }); |
| 15 | + resetForm(); |
| 16 | + generateReport(); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +// This function clears the values of the name, gender, age, and condition fields in the HTML form by setting them to empty strings or unchecked for radio buttons, effectively resetting the form to its initial state. |
| 21 | +function resetForm() { |
| 22 | + console.log("patients", patients); |
| 23 | + document.getElementById("name").value = ""; |
| 24 | + document.querySelector('input[name="gender"]:checked').checked = false; |
| 25 | + document.getElementById("age").value = ""; |
| 26 | + document.getElementById("condition").value = ""; |
| 27 | +} |
| 28 | + |
| 29 | +// This function calculates and constructs an analysis report based on the collected patient data stored in the patients[] array. |
| 30 | +function generateReport() { |
| 31 | + // Represents the total number of patients stored in the patients[] array |
| 32 | + const numPatients = patients.length; |
| 33 | + |
| 34 | + // A data structure (object) initializing counters for specific medical conditions (Diabetes, Thyroid, High Blood Pressure), initially set to zero. |
| 35 | + const conditionsCount = { |
| 36 | + Diabetes: 0, |
| 37 | + Thyroid: 0, |
| 38 | + "High Blood Pressure": 0, |
| 39 | + }; |
| 40 | + // A nested object with gender-specific condition counters ( male and female) for each medical condition, also initialized to zero for each condition. |
| 41 | + const genderConditionsCount = { |
| 42 | + Male: { |
| 43 | + Diabetes: 0, |
| 44 | + Thyroid: 0, |
| 45 | + "High Blood Pressure": 0, |
| 46 | + }, |
| 47 | + Female: { |
| 48 | + Diabetes: 0, |
| 49 | + Thyroid: 0, |
| 50 | + "High Blood Pressure": 0, |
| 51 | + }, |
| 52 | + }; |
| 53 | + |
| 54 | + for (const patient of patients) { |
| 55 | + conditionsCount[patient.condition]++; |
| 56 | + console.log("conditionsCount", conditionsCount[patient.condition]++); |
| 57 | + |
| 58 | + genderConditionsCount[patient.gender][patient.condition]++; |
| 59 | + console.log( |
| 60 | + "genderConditionsCount", |
| 61 | + genderConditionsCount[patient.gender][patient.condition]++ |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + report.innerHTML = `Number of patients: ${numPatients}<br><br>`; |
| 66 | + report.innerHTML += `Conditions Breakdown:<br>`; |
| 67 | + for (const condition in conditionsCount) { |
| 68 | + report.innerHTML += `${condition}: ${conditionsCount[condition]}<br>`; |
| 69 | + } |
| 70 | + |
| 71 | + report.innerHTML += `<br>Gender-Based Conditions:<br>`; |
| 72 | + for (const gender in genderConditionsCount) { |
| 73 | + report.innerHTML += `${gender}:<br>`; |
| 74 | + for (const condition in genderConditionsCount[gender]) { |
| 75 | + report.innerHTML += ` ${condition}: ${genderConditionsCount[gender][condition]}<br>`; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +addPatientButton.addEventListener("click", addPatient); |
| 81 | + |
| 82 | +// This JavaScript function is designed to work within a web page to retrieve health condition information based on user input. |
| 83 | +function searchCondition() { |
| 84 | + const input = document.getElementById("conditionInput").value.toLowerCase(); |
| 85 | + const resultDiv = document.getElementById("result"); |
| 86 | + resultDiv.innerHTML = ""; |
| 87 | + |
| 88 | + const patient = patients.find((item) => item.name.toLowerCase() === input); |
| 89 | + |
| 90 | + if (patient) { |
| 91 | + resultDiv.innerHTML += `<p><strong>Name:</strong> ${patient.name}</p>`; |
| 92 | + resultDiv.innerHTML += `<p><strong>Age:</strong> ${patient.age}</p>`; |
| 93 | + resultDiv.innerHTML += `<p><strong>Condition:</strong> ${patient.condition}</p>`; |
| 94 | + resultDiv.innerHTML += `<p><strong>Gender:</strong> ${patient.gender}</p>`; |
| 95 | + } else { |
| 96 | + resultDiv.innerHTML = "Condition not found."; |
| 97 | + } |
| 98 | +} |
| 99 | +btnSearch.addEventListener("click", searchCondition); |
0 commit comments