2

I would like to toggle between degree's and fahrenheit when the temperature is clicked.

I have managed to do this when it is clicked on degree's it is changed to fahrenheit, but now how do i change it back to degree's when clicked on fahrenheit?

    temp.addEventListener('click', degreeToF);

    function degreeToF() {
     const f = manchester.current.temp_c * 1.8 + 32;
     temp.innerHTML = f.toFixed(0) + '<span class="degrees"> f </span>';
    }

Here is my codepen: https://codepen.io/o-sewell/pen/mRyEyW

0

2 Answers 2

3
var showing = 'F';
temp.addEventListener('click', degreeToF);

function degreeToF() {
 if(showing === 'F'){
   // convert to C
   showing = 'C';
   const f = (manchester.current.temp_c - 32 ) * 5/9;
   temp.innerHTML = f.toFixed(0) + '<span class="degrees"> c </span>';
 } else {
   // convert to 
   showing = 'F';
   const f = manchester.current.temp_c * 1.8 + 32;
   temp.innerHTML = f.toFixed(0) + '<span class="degrees"> f </span>';

 }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this helped me understand :-)
2

Here you go. Used simple boolean value to tell the function which part of code to execute.

CodePen link

const weather = 'https://api.apixu.com/v1/current.json?key=cd93499e97644fcc873154715163112&q=Manchester';
const baseColors = ["#C2272D", "#F8931F", "#FFFF01", "#009245", "#0193D9", "#0C04ED", "#612F90"];
const tintColors = ["#F8DDDE", "#FEDBB4", "white", "#0193D9", "#009245", "#E7E6F9"];

let manchester = [];

fetch(weather)
  .then((blob) => blob.json())
  .then((data) => manchester = data)
  .then((data) => displayWeather(data));

let iconWeather = document.querySelector('#weather');
let temp = document.querySelector('#temp');
let textLocation = document.querySelector('#text-location');
let textWeather = document.querySelector('#text-weather');


function displayWeather() {
  iconWeather.src = manchester.current.condition.icon;
  temp.innerHTML = manchester.current.temp_c + '<span class="degrees"> c </span>';
  textLocation.innerHTML = manchester.location.name;
  textWeather.innerHTML = manchester.current.condition.text;
};

const background = document.querySelector('.weather');

window.addEventListener('load', changeBackground);

function changeBackground() {
  let random = Math.floor(Math.random() * baseColors.length);
  let randomBaseColor = baseColors[random];
  let randomTintColor = tintColors[random];
  background.style.background = 'linear-gradient(0deg,' + randomBaseColor + ',' + randomTintColor + ')';
  background.style.transition = 'background , 2s, ease';
}

setInterval(changeBackground, 2500);


temp.addEventListener('click', degreeToF);

var x = true;

function degreeToF() {

  if (x) {
    const f = manchester.current.temp_c * 1.8 + 32;
    temp.innerHTML = f.toFixed(0) + '<span class="degrees"> f </span>';
    x = !x;
  } else {
    const f = manchester.current.temp_c;
    temp.innerHTML = f.toFixed(0) + '<span class="degrees"> c </span>';
    x = !x;
  }

}
* {
  box-sizing: border-box;
}
.wrapper {
  margin: 50px;
}
.weather {
  max-width: 90%;
  margin: 0 auto;
  background: pink;
  padding: 20px;
  box-shadow: 0 5px rgba(0, 0, 0, 0.1);
  border-radius: 6px;
}
@media (min-width: 800px) {
  .weather {
    max-width: 40%;
  }
}
.weather__temperature {
  margin-top: 50px;
  text-align: center;
}
.weather__temperature--temp {
  font-size: 80px;
  cursor: pointer;
}
.weather__text {
  text-align: center;
}
.weather__text--description {
  color: black;
  font-size: 18px;
}
.weather__icon {
  margin-top: 5px;
}
.weather__icon--image {
  display: block;
  margin: 0 auto;
  padding: 5px 0;
  width: 150px;
  height: auto;
}
.weather__location {
  text-align: center;
}
.weather__location--text {
  letter-spacing: 5px;
  font-size: 22px;
  margin-bottom: 50px;
}
.degrees {
  color: red;
  font-size: 20px;
}
<div class="wrapper">
  <div class="weather">

    <div class="weather__temperature" />
    <p class="weather__temperature weather__temperature--temp" id="temp"></p>
  </div>

  <div class="weather__text">
    <p class="weather__text weather__text--description" id="text-weather"></p>
  </div>

  <div class="weather__icon">
    <img class="weather__icon weather__icon--image" id="weather" src="" />
  </div>


  <div class="weather__location">
    <p class="weather__location--text" id="text-location"></p>
  </div>

</div>
</div>

1 Comment

Thank you K. Daniek :-) i like how you used the boolean value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.