Build a Weather App - Build a Weather App

Tell us what’s happening:

Test 24 and 25 is failing, I tried deleting one character on API link, the console.log in getWeather and alert in showWeather worked but not when the links is correct

Your code so far

<!-- file: index.html -->
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Weather App</title>
    <link rel="stylesheet" href="styles.css">
  </head>

  <body>
    <main>
      <div class="weather-display">
        <!-- <h1 id="location">Location</h1>
        <img src="https://cdn.freecodecamp.org/weather-icons/01n.png">
        <p id="weather-main">Clear</p>
        <p id="main-temperature">35°C</p>
        <p id="feels-like">30°C</p>
        <p id="humidity">50%</p>
        <p id="wind">4m/s</p>
        <p id="wind-gust">6m/s</p> -->
      </div>
      <div class="controls">
        <h2>Weather for:</h2>
        <select>
          <option value=""></option>
          <option value="new york">New York</option>
          <option value="los angeles">Los Angeles</option>
          <option value="chicago">Chicago</option>
          <option value="paris">Paris</option>
          <option value="tokyo">Tokyo</option>
          <option value="london">London</option>
        </select>
        <button type="button" id="get-weather-btn">Get weather</button>
      </div>
    </main>
    <script src="script.js"></script>
  </body>
</html>
/* file: styles.css */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

main {
  background-color: lightblue;
  height: 100vh;
  width: 100vw;
  padding: 20px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.weather-display {
  width: clamp(300px, 100%, 800px);
  height: clamp(200px, 80%, 600px);
  border: 5px solid gray;
  border-radius: 10px;
  padding: 20px;
  margin: 10px;
  
}

.controls {
  width: clamp(300px, 100%, 800px);
  height: clamp(100px, 20%, 300px);
  border: 5px solid gray;
  border-radius: 10px;
  padding: 20px;
  margin: 10px;
  display: flex;
  justify-content: space-evenly;
  align-items: center;

  h1 {
    font-size: 20px;
  }
}
/* file: script.js */
const weatherDisplay = document.querySelector(".weather-display");
const selectedCity = document.querySelector("select");
const weatherBtn = document.getElementById("get-weather-btn");

weatherBtn.addEventListener("click", () => {
  showWeather(selectedCity.value);
})

async function getWeather(city){

  try {
    const res = await fetch(`https://weather-proxy.freecodecamp.rocks/api/city/${city}`);
    const data = await res.json();
    return data;
  } catch(error) {
    console.log(error)
    throw error;
  }
}

async function showWeather(city) {
  if (!city) {
    return
  }
  let data;
  try {
    data = await getWeather(city);
  } catch(error) {
    alert("Something went wrong, please try again later");
    return
  }

  const display = `
    <h1 id="location">${data.name ? data.name : "N/A"}</h1>
    <img id="weather-icon" src="${data.weather[0].icon ? data.weather[0].icon : "N/A"}">
    <p id="weather-main">${data.weather[0].main ? data.weather[0].main : "N/A"}</p>
    <p id="main-temperature">${data.main.temp ?? "N/A"}°C</p>
    <p id="feels-like">${data.main.feels_like ?? "N/A"}°C</p>
    <p id="humidity">${data.main.humidity ?? "N/A"}%</p>
    <p id="wind">${data.wind.speed ?? "N/A"}m/s</p>
    <p id="wind-gust">${data.wind.gust ?? "N/A"}m/s</p>
  `;

  weatherDisplay.innerHTML = display;
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/154.0.0.0 Safari/537.36

Challenge Information:

Build a Weather App - Build a Weather App

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-weather-app/66f12a88741aeb16b9246c59.md at main · freeCodeCamp/freeCodeCamp · GitHub

Welcome back to the forum @jemmelfloranza

If there is an error, your getWeather function should log the error to the console.

You are not asked to throw an error.

Happy coding

Thanks! I removed throw error in getWeather and try block in showWeather and it worked