Build a Weather App - Build a Weather App

Tell us what’s happening:

Failing test 25 even though when I try, it behaves as expected.

The test:
25. When Paris is selected the app should show an alert with Something went wrong, please try again later.

But when I select Paris myself, the alert pops up with the desired message.

Your code so far

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

  <body>
    <main>
      <div id="weather-info-container">
        <h2 id="location"></h2>
        <hr>
        <div id="info-grid">
          <p id="main-temperature"></p>
          <div>
            <img id="weather-icon">
            <span id="weather-main"></span>
          </div>
          <div>Feels like: <span id="feels-like"></span></div>
          <div>Humidity: <span id="humidity"></span></div>
          <div>Wind: <span id="wind"></span></div>
          <div>Gusts: <span id="wind-gust"></span></div>
          <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" transform="rotate(0)" height="50" id="direction"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M12 4V20M12 4L8 8M12 4L16 8" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> </g></svg>
        </div>
      </div>
      <select id="select-city">
        <option value="" selected></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 id="get-weather-btn">Get Weather</button>
    </main>
  </body>
  <script src="script.js"></script>
</html>


/* file: script.js */
const selectCity = document.getElementById("select-city");
const getWeatherBtn = document.getElementById("get-weather-btn");
const locationContainer = document.getElementById("location");
const mainTemp = document.getElementById("main-temperature");
const weatherIcon = document.getElementById("weather-icon");
const feelsLike = document.getElementById("feels-like");
const humidity = document.getElementById("humidity");
const wind = document.getElementById("wind");
const windGust = document.getElementById("wind-gust");
const weatherMain = document.getElementById("weather-main");
const direction = document.getElementById("direction");

async function getWeather (city) {
  if (city === '') {
    return -2;
  }
  try {
    const response = await fetch(`https://weather-proxy.freecodecamp.rocks/api/city/${city}`);
    return await response.json();
  }
  catch (e) {
    console.log(e.message);
    return e;
  }
}

async function showWeather (city) {
  if (city === '') {
    return;
  }
  const weatherData = await getWeather(city);
  if (weatherData.error || weatherData.message) {
    alert("Something went wrong, please try again later");
    return;
  }
  locationContainer.textContent = weatherData.name;
  mainTemp.textContent = weatherData.main.temp ? weatherData.main.temp + "° C" : "N/A";
  weatherIcon.setAttribute("src", weatherData.weather[0].icon);
  feelsLike.textContent = weatherData.main.feels_like ? weatherData.main.feels_like + "° C" : "N/A";
  humidity.textContent = weatherData.main.humidity ? weatherData.main.humidity + "%" : "N/A";
  wind.textContent = weatherData.wind.speed ? weatherData.wind.speed + " m/s" : "N/A";
  windGust.textContent = weatherData.wind.gust ? weatherData.wind.gust + " m/s" : "N/A";
  weatherMain.textContent = weatherData.weather[0].main ? weatherData.weather[0].main : "N/A";
  direction.style.transform = `rotate(${weatherData.wind.deg}deg)`
}


getWeatherBtn.addEventListener("click", () => {
  showWeather(selectCity.value);
});
/* file: styles.css */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  background-color: grey;
  font-family: sans-serif;
}
main {
  max-width: 500px;
  height: 400px;
  text-align: center;
  margin: 10px auto;
  border: 1px solid black;
  background-color: white;
  border-radius: 5px;
}
#weather-info-container {
  border: 1px solid grey;
  margin: 10px auto;
  width: 95%;
  height: 80%;
  background-color: #ddd;
  padding: 5px 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding-top: 10px;
}
#info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  height: 80%;
  gap: 5px;
  padding-top: 10px;
  p, span {
    font-size: 1.7rem;
  }
}
#weather-icon {
  margin: auto;
}
#main-temperature {}
#feels-like {}
#humidity {}
#wind {}
#wind-gust {}
#location {
  margin-bottom: 10px;
}
#direction {
  margin: auto;
}

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:157.0) Gecko/20100101 Firefox/157.0

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

Hi @youssie,

Consider where you add the “something went wrong” alert.

For example, would your code catch an error here if there was something wrong with response? Should it even attempt response.json() in that case?

As an aside, this code is redundant since you already check for an empty city value in showWeather before you call getWeather…and -2??.

Happy coding