-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
62 lines (50 loc) · 1.89 KB
/
Copy pathscript.js
File metadata and controls
62 lines (50 loc) · 1.89 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
// Get references to DOM elements
const form = document.getElementById('gender-form');
const input = document.getElementById('name-input');
const resultBox = document.getElementById('result');
// Listen for form submission
form.addEventListener('submit', async (e) => {
e.preventDefault(); // Prevent page refresh
// Get and clean the name entered by the user
const name = input.value.trim();
// Validation: If input empty, show error message
if (!name) {
resultBox.textContent = "Please enter your name.";
resultBox.style.color = "red";
return;
}
// Show temporary loading message
resultBox.style.color = "black";
resultBox.textContent = "Predicting ...";
try {
// Make API call to Genderize API with the entered name
const response = await fetch(`https://api.genderize.io/?name=${name}`);
const data = await response.json();
// If no prediction is available for the name
if (!data.gender) {
resultBox.textContent = `No prediction found for ${name}!`;
resultBox.style.color = "gray";
return;
}
// Extract gender and probability values
const gender = data.gender;
const probability = (data.probability * 100).toFixed(2);
// Display prediction result
resultBox.textContent = `Predicted Gender: ${gender.toUpperCase()} (${probability}%)`;
// Add color styling based on predicted gender
if (gender === "male") resultBox.style.color = "blue";
else if (gender === "female") resultBox.style.color = "deeppink";
// Fade-in animation effect
resultBox.style.opacity = 0;
setTimeout(() => {
resultBox.style.opacity = 1;
}, 100);
// Clear input field after showing result
input.value = "";
} catch (error) {
// Handles network or fetch errors
resultBox.textContent = "Error fetching data. Please try again later.";
resultBox.style.color = "red";
console.error(error);
}
});