Advertisement
r5sqmk83

single-page weather application

Jul 8th, 2025
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | Software | 0 0
  1. ## Generate the HTML, CSS, and JavaScript code for a single-page weather application.
  2.  
  3. **Functionality:**
  4.  
  5. 1. Display an input field prominently labeled "Enter City Name" where the user can type a city.
  6. 2. Display a button labeled "Get Weather".
  7. 3. When the button is clicked:
  8. * Display a simple loading indicator (e.g., text like "Loading...") while the data is being fetched.
  9. * Fetch the current weather data for the entered city using the WeatherAPI `current.json` endpoint (`https://api.weatherapi.com/v1/current.json`).
  10. * Assume the necessary WeatherAPI API key is securely stored and accessible within the application's environment, represented in the JavaScript code by a constant named `APIKEY`. **Do not hardcode the key directly in the script.**
  11. * Once the data is received or an error occurs, hide the loading indicator.
  12. * If the API call is successful, display the following information clearly in a designated results area:
  13. * City Name (as returned by the API for confirmation)
  14. * Current Temperature (in Celsius °C)
  15. * Humidity (in %)
  16. * Wind Speed (in kilometers per hour, kph)
  17. * Implement robust error handling: If the city is not found, the API key is invalid, or any other network/API error occurs, display a user-friendly error message (e.g., "Error: Could not retrieve weather data for [City Name]. Please verify the city name or check API key configuration."). Clear any previously displayed weather data when an error occurs.
  18.  
  19. **Styling:**
  20.  
  21. * Apply a clean, modern, and visually appealing style using CSS.
  22. * Ensure the layout is responsive and looks good on both desktop and mobile screens.
  23. * Center the core elements (input field, button, results area) on the page.
  24. * Use clear typography, appropriate spacing, and padding for readability.
  25. * Style the results area distinctly (e.g., using a card-like background or border).
  26.  
  27. **Code Structure:**
  28.  
  29. * Provide the complete code structured into three separate parts:
  30. 1. HTML (`index.html`): Define the structure with semantic elements (e.g., `<main>`, `<form>`, `<label>`, `<input>`, `<button>`, `<div>` for results/errors/loading).
  31. 2. CSS (`style.css`): Define all the styles for layout, appearance, and responsiveness.
  32. 3. JavaScript (`script.js`): Implement the logic for handling user input, button clicks, API calls (using `fetch`), updating the DOM, and error handling.
  33.  
  34. **Technical Requirements & Best Practices:**
  35.  
  36. * Use vanilla JavaScript (ES6+) without external libraries or frameworks (unless essential for specific advanced features not requested here).
  37. * Use the modern `fetch` API with `async/await` for handling the asynchronous API request.
  38. * Ensure the JavaScript code is unobtrusive, separating logic from the HTML structure.
  39. * Add comments in the JavaScript code to explain key parts, especially the API call and data handling logic.
  40. * Prioritize basic accessibility: Use appropriate ARIA attributes if necessary, ensure keyboard navigability, and maintain good color contrast.
  41.  
  42. You must use this key:
  43. WEATHER_API_KEY=8c95...
  44.  
  45. API Documentation which you can access using the MCP fetch tool:
  46. https://www.weatherapi.com/docs/
  47.  
  48. Example call:
  49. https://api.weatherapi.com/v1/current.json?key=8c95...&q=London&aqi=no
  50.  
  51. Example Response Body:
  52.  
  53. ```
  54. {
  55. "location": {
  56. "name": "London",
  57. "region": "City of London, Greater London",
  58. "country": "United Kingdom",
  59. "lat": 51.5171,
  60. "lon": -0.1062,
  61. "tz_id": "Europe/London",
  62. "localtime_epoch": 1744266568,
  63. "localtime": "2025-04-10 07:29"
  64. },
  65. "current": {
  66. "last_updated_epoch": 1744265700,
  67. "last_updated": "2025-04-10 07:15",
  68. "temp_c": 7.3,
  69. "temp_f": 45.1,
  70. "is_day": 1,
  71. "condition": {
  72. "text": "Overcast",
  73. "icon": "//cdn.weatherapi.com/weather/64x64/day/122.png",
  74. "code": 1009
  75. },
  76. "wind_mph": 4.3,
  77. "wind_kph": 6.8,
  78. "wind_degree": 23,
  79. "wind_dir": "NNE",
  80. "pressure_mb": 1032.0,
  81. "pressure_in": 30.47,
  82. "precip_mm": 0.0,
  83. "precip_in": 0.0,
  84. "humidity": 70,
  85. "cloud": 100,
  86. "feelslike_c": 6.1,
  87. "feelslike_f": 43.0,
  88. "windchill_c": 4.6,
  89. "windchill_f": 40.3,
  90. "heatindex_c": 6.0,
  91. "heatindex_f": 42.8,
  92. "dewpoint_c": 2.6,
  93. "dewpoint_f": 36.8,
  94. "vis_km": 10.0,
  95. "vis_miles": 6.0,
  96. "uv": 0.1,
  97. "gust_mph": 5.6,
  98. "gust_kph": 9.0
  99. }
  100. }
  101. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement