-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassessment.js
More file actions
206 lines (187 loc) · 7.06 KB
/
assessment.js
File metadata and controls
206 lines (187 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
let currentQuestionIndex = 0;
const responses = {};
const questions = [
{
type: "dates",
question: "What were the dates of your last three menstrual periods?",
subfields: ["Most Recent", "Previous", "Third Most Recent"]
},
{
type: "scale",
question: "How would you rate the amount of excessive hair growth on your body (e.g., face, chest, back)?",
image: "images/hirsutism-scale.jpg",
options: [1, 2, 3, 4, 5],
labels: ["None", "Mild", "Moderate", "Severe", "Very Severe"]
},
{
type: "scale",
question: "How would you rate the severity of acne or oily skin?",
image: "images/acne-scale.jpg",
options: [1, 2, 3, 4, 5],
labels: ["None", "Mild", "Moderate", "Severe", "Very Severe"]
},
{
type: "scale",
question: "How noticeable is your hair thinning or male-pattern baldness?",
image: "images/hairloss-scale.jpg",
options: [1, 2, 3, 4, 5],
labels: ["None", "Mild", "Moderate", "Severe", "Very Severe"]
},
{
type: "measurements",
question: "What is your height and weight?",
fields: [
{ label: "Height", type: "number", unit: ["cm", "inches"] },
{ label: "Weight", type: "number", unit: ["kg", "pounds"] }
]
},
{
question: "Have you noticed skin darkening in body folds?",
type: "yesno",
help: "Common areas include neck folds, groin, or underarms"
},
{
question: "Please enter your body measurements",
type: "measurements",
fields: [
{
label: "Waist",
unit: ["cm", "inches"]
},
{
label: "Hip",
unit: ["cm", "inches"]
}
]
},
{
type: "yesno",
question: "Is there a history of PCOS or diabetes in your family?"
}
]
const questionContainer = document.getElementById('question-container');
const progressBar = document.getElementById('progress-bar');
const prevButton = document.getElementById('prev-btn');
const nextButton = document.getElementById('next-btn');
document.addEventListener('DOMContentLoaded', () => {
loadQuestion(currentQuestionIndex);
updateNavigationButtons();
});
prevButton.addEventListener('click', () => {
if (currentQuestionIndex > 0) {
currentQuestionIndex--;
loadQuestion(currentQuestionIndex);
updateNavigationButtons();
}
});
nextButton.addEventListener('click', () => {
if (currentQuestionIndex < questions.length - 1) {
saveResponse();
currentQuestionIndex++;
loadQuestion(currentQuestionIndex);
} else {
saveResponse();
submitAssessment();
}
updateNavigationButtons();
});
function saveResponse() {
const question = questions[currentQuestionIndex];
switch(question.type) {
case "dates":
responses[currentQuestionIndex] = question.subfields.reduce((acc, field) => {
const inputName = `period_${field.toLowerCase().replace(' ', '_')}`;
acc[field] = document.querySelector(`input[name="${inputName}"]`).value;
return acc;
}, {});
break;
case "scale":
responses[currentQuestionIndex] = document.querySelector('input[name="response"]:checked')?.value;
break;
case "measurements":
responses[currentQuestionIndex] = question.fields.reduce((acc, field) => {
const value = document.querySelector(`input[name="${field.label.toLowerCase()}"]`).value;
const unit = document.querySelector(`select[name="${field.label.toLowerCase()}_unit"]`).value;
acc[field.label] = { value, unit };
return acc;
}, {});
break;
case "yesno":
responses[currentQuestionIndex] = document.querySelector('input[name="response"]:checked')?.value;
break;
}
}
function updateNavigationButtons() {
prevButton.disabled = currentQuestionIndex === 0;
nextButton.textContent = currentQuestionIndex === questions.length - 1 ? 'Submit' : 'Next';
}
function submitAssessment() {
if (!validateAllResponses()) {
alert("Please complete all measurements");
return;
}
console.log("Submitting responses:", responses);
window.location.href = `results.html?data=${encodeURIComponent(JSON.stringify(responses))}`;
}
function validateAllResponses() {
const measurementResponses = responses[6];
if (!measurementResponses?.Waist?.value || !measurementResponses?.Hip?.value) {
return false;
}
return true;
}
function loadQuestion(index) {
const questionData = questions[index];
let html = `<div class="question-card">
<h2>${questionData.question}</h2>`;
switch(questionData.type) {
case "dates":
html += `<div class="dates-container">
${questionData.subfields.map(field => `
<div class="date-field">
<label>${field}</label>
<input type="date" name="period_${field.toLowerCase().replace(' ', '_')}">
</div>
`).join('')}
</div>`;
break;
case "scale":
html += `
<div class="scale-container">
${questionData.image ? `<img src="${questionData.image}" alt="Scale reference">` : ''}
<div class="scale-options">
${questionData.options.map((value, i) => `
<label class="scale-option">
<input type="radio" name="response" value="${value}">
<span class="scale-value">${value}</span>
<span class="scale-label">${questionData.labels[i]}</span>
</label>
`).join('')}
</div>
</div>`;
break;
case "measurements":
html += `<div class="measurements-container">
${questionData.fields.map(field => `
<div class="measurement-field">
<label>${field.label}</label>
<input type="number" name="${field.label.toLowerCase()}" step="0.1">
<select name="${field.label.toLowerCase()}_unit">
${field.unit.map(u => `<option value="${u}">${u}</option>`).join('')}
</select>
</div>
`).join('')}
</div>`;
break;
case "yesno":
html += `<div class="yesno-container">
<label><input type="radio" name="response" value="yes">Yes</label>
<label><input type="radio" name="response" value="no">No</label>
</div>`;
break;
}
html += `</div>`;
questionContainer.innerHTML = html;
progressBar.style.width = `${((index + 1) / questions.length) * 100}%`;
}
loadQuestion(currentQuestionIndex);