Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="container">
  <h1>freeCodeCamp<br>Weather App!</h1>
  <p id="location"></p>
  <p id="tempContainer"><span id="temp"></span>&nbsp;°<span id="units"></span></p>
  <p id="condition"></p>
  <div id="iconContainer">
    <i id="icon" class="wi"></i>
  </div>
  
</div>
              
            
!

CSS

              
                body {
  background-color: #020300;
  padding: 0;
  font-size: 10px;
}

#container {
  background-color: #4A4063;
  color: #D6C3C9;
  opacity: 0.8;
  font-size: 2rem;
  width: 50%;
  margin: 1% auto;
  padding: 1% 0;
  text-align: center;
  border-radius: 13px;
  border: 2px solid #D6C3C9;
}

h1 {
  margin-top: 0;
  font-size: 3rem;
  text-shadow: #59544B 0 0 15px;
}

p {
  margin: 0;
  padding: 0;
  line-height: 2.3rem;
}

#units {
  text-decoration: underline;
  font-weight: bold;
  cursor: pointer;
}

@keyframes rotateIn {
  to {
    transform: rotateY(180deg);
  }
}

#icon {
  transform-origin: center;
/*   transform: rotateY(-180deg); */
  animation: 4s ease infinite normal rotateIn;
  font-size: 40px;
}

div#iconContainer {
  margin: 42px 0 10px;
}
              
            
!

JS

              
                (function() {
  const apiKey = "cf4fc1f94c8da0ae6532afeb64e38115";
  let baseReqURL = "api.openweathermap.org/data/2.5/weather?lat=35&lon=139";
  let payload = null;

  let lat = "";
  let lon = "";
  let currentUnits = "F";
  let currentTemp = null;
  let httpRequest;
  
  // Save selector references to variables   
  let location = document.querySelector("#location");
  let country = document.querySelector("#country");
  let tempContainer = document.querySelector("#tempContainer");
  let temp = document.querySelector("#temp");
  let units = document.querySelector("#units");
  let condition = document.querySelector("#condition");
  let iconContainer = document.querySelector("#iconContainer");
  let icon = document.querySelector("#icon");
    
  function success(position) {
    lat = position.coords.latitude;
    lon = position.coords.longitude;
    httpRequest = new XMLHttpRequest();
    httpRequest.onreadystatechange = handleResponse;
    httpRequest.open('GET', `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&APPID=${apiKey}`, true);
    httpRequest.send();
  }

  // This function will run if the AJAX call is unsuccessful.
  function failure() {
    console.log("Geolocation failed! [Insert sad face emoticon]");
  }

  function handleResponse() {
    if (httpRequest.readyState === XMLHttpRequest.DONE) {
      if (httpRequest.status === 200) {
        showWeatherData();
      } else {
        alert("Error! Try again.");
      }
    } else {
      
    }
  }

  // Fill in the data containers on the page with corresponding
  // text from the corresponding JSON payload returned from AJAX
  // call.
  function showWeatherData() {
    //convert string response to JSON and save to variable
    payload = JSON.parse(httpRequest.responseText);
    
    //convert temp from K to default unit, F
    currentTemp = Math.round((9/5) * (payload.main.temp - 273) + 32);
    
    location.innerHTML = payload.name + ", " + payload.sys.country;
    showTemp();
    condition.innerHTML = payload.weather[0].main;
    showIcon();
  }
  
  function convertUnits() {
    if (currentUnits === "F") {
      currentUnits = "C";
      // C = 5/9 (° F - 32)
      currentTemp = Math.round(payload.main.temp - 273);
      showTemp();
    } else {
      currentUnits = "F";
      currentTemp = Math.round((9/5) * (payload.main.temp - 273) + 32);
      showTemp();
    }
  }
  
  function showTemp() {
    temp.innerHTML = currentTemp;
    units.innerHTML = currentUnits;
  }
  
  function showIcon() {
    let iconClass = "wi-owm-" + payload.weather[0].id;
    icon.classList.add(iconClass);
  }
  
  units.addEventListener("click", convertUnits);
  
  navigator.geolocation.getCurrentPosition(success, failure);
})();
              
            
!
999px

Console