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

              
                <body>
  <header>
    <div id="top">
      <p>Learning jQuery, JSON and APIs</p>
    </div>
    <div>
      <h1>Weather Forecast</h1></div>

  </header>
  <main>
    <section id="featuresSec">
      <div id="featuresDiv" class="container">
        <p><br>Displays weather of current location | Switch between celsius and farenheit | Enter a city name to display weather for that location</p>
        <hr>
      </div>
    </section>
    <br>
    <section id="inputSec">
      <div id="inputDiv" class="container">
        <br>
        <input type="text" id="userInput" placeholder="Enter city or country">
        <button id='goButton'>Go</button>
        <br><br>
      </div>
    </section>

    <br>

    <section id="weatherSec">
      <div id="weatherDiv" class="container">
        <br>
        <h1 id="location" class="uppercase"></h1>
        <div class="row">
          <div id="primaryWeatherDiv" class="col-6">
            <span id="icon"></span>
            <h2 id="mainWeather" class="uppercase"></h2>
            <h2>
	  							<span id="temp" class="uppercase"></span> <a href="#" id="cf" title="Switch between Celsius and Fareheit"><span id="c" class="selected">C</span> | <span id="f" class="unselected">F</span></a>
	  						</h2>
            <br>
          </div>
          <div id="secondaryWeatherDiv" class="col-6">
            <p>
              <span id="day"></span> <span id="time"></span><br>
              <span id="descWeather" class="uppercase"></span><br><br>
              <span id="wind"></span><br>
              <span id="humidity"></span>
            </p>
          </div>
        </div>
      </div>
    </section>

    <br>
          <section id="forecastSec">
               <div id="forecastDiv" class="container">
                    <hr>
                    <div class="row">
                         <div class="col-3 forecast fDay"></div>
                         <div class="col-3 forecast fIcon"></div>
                         <div class="col-3 forecast fDesc uppercase"></div>
                         <div class="col-3 forecast fTemp"></div>
                    </div>
                    <hr>
                    <div class="row">
                         <div class="col-3 forecast fDay"></div>
                         <div class="col-3 forecast fIcon"></div>
                         <div class="col-3 forecast fDesc uppercase"></div>
                         <div class="col-3 forecast fTemp"></div>
                    </div>
                    <hr>
                    <div class="row">
                         <div class="col-3 forecast fDay"></div>
                         <div class="col-3 forecast fIcon"></div>
                         <div class="col-3 forecast fDesc uppercase"></div>
                         <div class="col-3 forecast fTemp"></div>
                    </div>
                    <hr>
                    <div class="row">
                         <div class="col-3 forecast fDay"></div>
                         <div class="col-3 forecast fIcon"></div>
                         <div class="col-3 forecast fDesc uppercase"></div>
                         <div class="col-3 forecast fTemp"></div>
                    </div>
                    <hr>
               </div>
          </section>

    <br>
  </main>

  <footer>
    <div>
      <p>Written by Suraj Radhakrishnan</p>
         <p id="credits">Thank you for your help <a href="https://forum.freecodecamp.org/u/pseudop/summary" target="_blank">pseudop</a></p>
    </div>    
  </footer>
</body>
              
            
!

CSS

              
                * {
     margin: 0;
     padding: 0;
}
html {
     height: 100%;
     box-sizing: border-box;
     position: relative;
}
body {
     text-align: center;
     font-family: Roboto, sans-serif;
     position: relative;
     min-height: 100%;
     padding-bottom: 250px;
     background-color: #feffff;
     color: #feffff;
}
header {
     color: #feffff;
     background-color: #444c5c;
     padding: auto;
}
h1 {
  margin:0;
}
#top {
     font-size: 12px;
     background-color: #2b3343;
}
#featuresSec {
  font-size: 12px;
     color:#444c5c;
}
#userInput {
     border: none;
     border-bottom: 1px solid #444c5c;
}
#goButton {
     width: 100px;
     border: none;
     background-color: #444c5c;
     color: #feffff;
     transition-duration: 0.3s;
}
#goButton:hover {
     background-color: #78a5a3;
}
#weatherDiv,
#forecastDiv {
     background-color: #78a5a3;
}
#cf {
     color: #feffff;
     text-decoration: none;
}
#cf:hover{
     transition-duration: 0.3s;
     color: #444c5c;
     text-decoration: none;
}
.selected {
     color: #feffff;
     font-weight: bold;
     transition-duration: 0.3s;
}
.unselected {
     font-weight: normal;
     font-size: 20px;
     color: #444c5c;
     transition-duration: 0.3s;
}
#location {
     text-align: center;
}
.uppercase {
     text-transform: uppercase;
}
#icon {
     text-align: right;
}
#primaryWeatherDiv {
     text-align: right;
}
#secondaryWeatherDiv {
     text-align: left;
}
p {
     padding: 0;
     margin: 0;
}
.forecast {
     margin: auto;
}
footer {
     background-color: #444c5c;
     color: #feffff;
     position: absolute;
     width: 100%;
     bottom: 0;
}

#credits {
     font-size:12px;
}
              
            
!

JS

              
                var ipinfoURL = "https://ipinfo.io/json";
var weatherURL = "https://api.openweathermap.org/data/2.5/weather?q=";
var forecastURL = "https://api.openweathermap.org/data/2.5/forecast?q=";
var userRegion;
var appID = "&appid=34d6b731f1daa7703ad067ef8704af2a";
var units = "&units=metric";
var wTemp;
var fTemp = [];
var unit = "celsius";

function getWeather(userRegion) {
  $.ajax({
    dataType: "json",
    url: weatherURL + userRegion + units + appID,
    success: function(wData) {
      displayWeather(wData);
      //console.log(weatherData);
    }
  });
}

function displayWeather(wData) {
  //primary weather information
  $("#location").text(userRegion + ", " + wData.sys.country);
  var imgsrc =
    "https://openweathermap.org/img/w/" + wData.weather[0].icon + ".png";
  $("#icon").html('<img src="' + imgsrc + '" class=".wIcon" alt="w-icon">');
  wTemp = Math.round(wData.main.temp);
  $("#temp").text(Math.round(wTemp));
  $("#mainWeather").text(wData.weather[0].main);
  $("#tempMax").text(wData.main.temp_max);
  $("#tempMin").text(wData.main.temp_min);
  //secondary weather information
  $("#day").text(dayOfWeek());
  $("#time").text(currentTime());
  $("#descWeather").text(wData.weather[0].description);
  $("#wind").text("Wind: " + wData.wind.speed);
  $("#humidity").text("Humidity: " + wData.main.humidity + "%");
}

function getForecast(userRegion) {
  $.ajax({
    dataType: "json",
    url: forecastURL + userRegion + units + appID,
    success: function(fData) {
      //console.log(fData);
      displayForecast(fData);
    }
  });
}

function displayForecast(fData) {
  var i = 8; //Used to iterate through JSON data

  $(".fDay").each(function() {
    $(this).text(fData.list[i].dt_txt);
    i += 8;
  });
  i = 8;
  $(".fIcon").each(function() {
    var imgsrc =
      "https://openweathermap.org/img/w/" +
      fData.list[i].weather[0].icon +
      ".png";
    //console.log(imgsrc);
    $(this).html('<img src="' + imgsrc + '" alt="f-icon">');
    i += 8;
  });
  i = 8;
  $(".fDesc").each(function() {
    $(this).text(fData.list[i].weather[0].main);
    i += 8;
  });
  i = 8;
  $(".fTemp").each(function() {
    fTemp.push(Math.round(fData.list[i].main.temp));
    //console.log(fTemp);
    $(this).text(Math.round(fData.list[i].main.temp) + " C");
    i += 8;
  });
  i = 8;
}

function dayOfWeek() {
  var dt = new Date();
  //console.log(dt);
  var today = dt.getDay();
  var days = [
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
  ];
  return days[today];
}

function currentTime() {
  var now = new Date();
  var hours = now.getHours();
  var mins = now.getMinutes();
  var ampm = "";
  //logic to convert 24hr format to 12hr and get am/pm correctly
  if (hours < 12) ampm = "am";
  else if (hours == 00) {
    ampm = "am";
    hours = 12;
  } else if (hours == 12) ampm = "pm";
  else {
    ampm = "pm";
    hours = hours - 12;
  }
  //logic to show minutes as 2 digits even when minutes is lesser than 10
  mins = "0" + mins; //adds a string '0' to mins value and converts the whole mins to string. ex: 4 becomes 04, 15 becomes 015.
  mins = mins.slice(-2); //saves mins string as a slice of last 2 characters only. ex: 04 becomes 04, 015 becomes 15
  var time = "";
  time = hours + ":" + mins + ampm;
  return time;
}

$("#cf").click(function(e) {
  e.preventDefault();
  if (unit == "celsius") {
    unit = "farenheit";
    wTemp = wTemp * 1.8 + 32;
    $(".fTemp").each(function(index) {
      fTemp[index] = fTemp[index] * 1.8 + 32;
      $(this).text(Math.round(fTemp[index]) + " F");
    });
    $("#f")
      .addClass("selected")
      .removeClass("unselected");
    $("#c")
      .removeClass("selected")
      .addClass("unselected");
    $("#temp").text(Math.round(wTemp));
  } else {
    unit = "celsius";
    wTemp = (wTemp - 32) / 1.8;
    $(".fTemp").each(function(index) {
      fTemp[index] = (fTemp[index] - 32) / 1.8;
      $(this).text(Math.round(fTemp[index]) + " C");
    });
    $("#c")
      .addClass("selected")
      .removeClass("unselected");
    $("#f")
      .removeClass("selected")
      .addClass("unselected");
    $("#temp").text(Math.round(wTemp));
  }
});

$("#goButton").click(function() {
  if ($("#userInput").val() === "") {
    alert("Please enter a location.");
  } else {
    userRegion = $("#userInput").val();
    getWeather(userRegion);
    getForecast(userRegion);
  }
});

$(document).ready(function() {
  $.ajax({
    dataType: "json",
    url: ipinfoURL,
    success: function(locationData) {
      userRegion = locationData.city;
      getWeather(userRegion);
      getForecast(userRegion);
    }
  });  
});
              
            
!
999px

Console