-3

I had a previous issue with this but I got it fixed now I'm having a new issue below is the code.

<!DOCTYPE html>
<html lang="en">
<title>Weather</title>
<head>
<script>
 
 var temp = prompt("Enter the temp from outside");
 var sky = prompt("tell us what it is like outside put ether sun, rain, snow (it is case sensitive so follow orders.)");

 function getHot(temp) {
  if (temp >= 100) {
  document.write ("Bet u wish you could go naked but shorts and T-shirt will do.");
  }
  else if (temp >= 60) {
  document.write ("Ok thats a bit better but i would say shorts and T-shirt would be good.")
  }
  else if (temp >= 40){
  document.write ("Getting a bit nippy out maybe get some pants and a jacket.")
  }
  else if (temp >= 0){
  document.write ("Sucks to be you right now put on you big boy pants get a sweater and put a heavy coat on.")
  }
  else if (temp <= -1){
  document.write ("Stay inside you fool theres no need to freeze to death.")
  }
  else {
  document.write ("you mess with me i mess with you refresh to do this right.")
  }
  }
 function getLight(sky) {
 if (sky = sun){
 document.write ("Enjoy the light get out your house it's good for you inless it's to hot or to cold then hide from the light.")
 }
 else if (sky = rain){
 document.write ("Can't beleave you have to ask this but get a umbrella and maybe a poncho.")
 }
 else if (sky = snow){
 document.write ("If you are afraid of getting wet then use a umbrella other then that bundle up.")
 }
 else {
 document.write ("not going to tell you again follow what i say refresh and do it again.")
 }
 }
 
 getHot(temp);
 getLight(sky);
 
</script>
</head>
</html>

Ok so it prompts the user for the 2 inputs but then it only displays the information for the temp input and I need it to display both any advice?

Rodrigo Taboada
  • 2,727
  • 4
  • 24
  • 27

2 Answers2

1

Here's a few of the major things to look over:

  • document.write is dangerous, you should be writing to the DOM instead. Check out this question from SO: What is the correct way to write HTML using Javascript?. If you haven't covered the DOM yet, I wouldn't worry about it just yet. Use document.write if your teacher tells you to. Just know its not proper in the real world.

  • Your getLight function is using assignment operators, not comparison operators. Saying sky = sun is the equivalent of saying 'set the variable sky to the value of the variable sun' (which leads to the next point). You need to be using comparision operators sky === sun.

  • Your 'getLight' is comparing the value of the sky variable not to the strings 'sun' or 'rain', but to undefined variables named sun, `rain, etc. You need to ensure you are wrapping your strings in quotes.

So all in all, it should resemble something like:

if (sky === 'sun'){
    //output is a DOM element. See the link above on how to access it, or just use document.write if thats what your teacher wants.
      output.innerHTML = "Enjoy the light get out your house it's good for you inless it's to hot or to cold then hide from the light."
    }
Community
  • 1
  • 1
Aweary
  • 2,302
  • 17
  • 26
0

You have 2 problems:

First, you need to use the equals comparison operator (variable == value) instead of a single equals sign, which just updates the value of the variable

Second, you need to put your strings (sun, snow, etc) in quotes so JavaScript knows to treat those values as strings, otherwise it assumes those are variable names.

var temp = prompt("Enter the temp from outside");
var sky = prompt("tell us what it is like outside put ether sun, rain, snow (it is case sensitive so follow orders.)");
function getHot(temp) {
    if (temp >= 100) {
    alert("Bet u wish you could go naked but shorts and T-shirt will do.");
    }
    else if (temp >= 60) {
    alert("Ok thats a bit better but i would say shorts and T-shirt would be good.")
    }
    else if (temp >= 40){
    alert("Getting a bit nippy out maybe get some pants and a jacket.")
    }
    else if (temp >= 0){
    alert("Sucks to be you right now put on you big boy pants get a sweater and put a heavy coat on.")
    }
    else if (temp <= -1){
    alert("Stay inside you fool theres no need to freeze to death.")
    }
    else {
    alert("you mess with me i mess with you refresh to do this right.")
    }
    }
function getLight(sky) {
if (sky == "sun"){
alert("Enjoy the light get out your house it's good for you inless it's to hot or to cold then hide from the light.")
}
else if (sky == "rain"){
alert("Can't beleave you have to ask this but get a umbrella and maybe a poncho.")
}
else if (sky = "snow"){
alert("If you are afraid of getting wet then use a umbrella other then that bundle up.")
}
else {
alert("not going to tell you again follow what i say refresh and do it again.")
}
}

getHot(temp);
getLight(sky);

Here's a link to a working version in JSFiddle: http://jsfiddle.net/6Lhm0u91/2/

Alex Glover
  • 759
  • 5
  • 15