User review
Name: Loading...
Location: Loading...
Comment: Loading...
Rating: Loading...
'☆'; // Empty star
}
return starHtml;
}
// Function to fetch and display user data
async function fetchRandomUser() {
try {
const response = await fetch('https://randomuser.me/api/');
const data = await response.json();
const user = data.results[0];
// Set the user info
document.getElementById('user-pic').src = user.picture.large;
document.getElementById('user-name').textContent = `${user.name.title} ${user.name.first} ${user.name.last}`;
document.getElementById('user-email').textContent = user.email;
document.getElementById('user-phone').textContent = user.phone;
document.getElementById('user-location').textContent = `${user.location.city}, ${user.location.state}, ${user.location.country}`;
// Random comment and rating
const randomComment = comments[Math.floor(Math.random() * comments.length)];
const randomRating = generateRandomStars();
// Set the review and rating
document.getElementById('user-review').textContent = randomComment;
document.getElementById('user-rating').innerHTML = displayStars(randomRating);
// Remove loading classes
document.querySelectorAll('.loading').forEach(el => el.classList.remove('loading'));
} catch (error) {
console.error("Failed to fetch user data:", error);
}
}
// Preload user data when the window starts loading
window.onload = fetchRandomUser;
]]>
0 Response to "Random User Generator"
Post a Comment