2

I am brand new to JavaScript and I have a homework assignment to convert a number prompt (Celsius) into Fahrenheit and Kelvin, but my Fahrenheight and Kelvin show up as undefined when I run the program, does anyone have a solution?

        var celcius;
        var fahrenheight;
        var kelvin;

        celcius = Number(window.prompt("Enter Degree", 50));
        document.write(celcius + " degrees Celcius.");
        
        function FahrenheightConvert() {
            var fahrenheight = Math.round(9 / 5 * (celcius + 32));
        }
        document.write("<br>");
        document.write(fahrenheight + " degrees Fahrenheight.");
        
        function KelvinConverter(){
            var kelvin = (celcius + 273.15);
        }
        document.write("<br>");
        document.write(kelvin + " in Kelvin.");
imvain2
  • 10,699
  • 1
  • 11
  • 19
  • 5
    Well you never call your functions... And she you do, you have an issue with variable scope. – epascarello Feb 14 '19 at 21:26
  • 5
    NB/ `document.write` will reset your page. Don't use it. Instead use `console.log`. – trincot Feb 14 '19 at 21:28
  • 1
    @trincot [Why is document.write considered a “bad practice”?](https://stackoverflow.com/q/802854/1048572) lists all the reasons - it's always a good link target on this topic :-) – Bergi Feb 14 '19 at 21:29
  • Great that you provided a link to an informative Q&A, @Bergi. – trincot Feb 14 '19 at 21:33
  • Note the conversion formula for Celcius to Fahrenheight needs to be corrected before submission - 0ºC is not 58ºF :-) – traktor Feb 14 '19 at 22:37

4 Answers4

1

The code inside a function doesn't run until you tell it to by calling the function. You never call any of the functions you've written, so the conversion code inside them is never going to run.

You can in this case just remove the functions completely, leaving you with this:

var celcius;
var fahrenheight;
var kelvin;

celcius = Number(window.prompt("Enter Degree", 50));
document.write(celcius + " degrees Celcius.");

fahrenheight = Math.round(9 / 5 * (celcius + 32));
document.write("<br>");
document.write(fahrenheight + " degrees Fahrenheight.");

kelvin = (celcius + 273.15);
document.write("<br>");
document.write(kelvin + " in Kelvin.");
Effective Robot
  • 206
  • 1
  • 5
1

Your problem is one of variable scope; you're not actually passing your celcius into either of your functions. In addition to this, you're not retrieving the values assigned to fahrenheight or kelvin in the functions for the same reason.

To correct this, you'll want to pass celcius as a function argument to both of your functions, and also make sure to actually invoke them:

var celcius;
var fahrenheight;
var kelvin;

celcius = Number(window.prompt("Enter Degree", 50));
document.write(celcius + " degrees Celcius.");

function FahrenheightConvert(celcius) {
  var fahrenheight = Math.round(9 / 5 * (celcius + 32));
  document.write(fahrenheight + " degrees Fahrenheight.");
}

document.write("<br>");
FahrenheightConvert(celcius);

function KelvinConverter(celcius) {
  var kelvin = (celcius + 273.15);
  document.write(kelvin + " in Kelvin.");
}

document.write("<br>");
KelvinConverter(celcius);

Alternatively, you could simply not use functions at all, which would have the same effect (as all variables would be in the same scope).

Obsidian Age
  • 36,816
  • 9
  • 39
  • 58
1

2 problems. 1) you are not using the global variable in your functions. You are recreating one in local scope. You can fix that by changing your functions to:

 function FahrenheightConvert() {
   fahrenheight = Math.round(9 / 5 * (celcius + 32));
 }

 function KelvinConverter(){
   kelvin = (celcius + 273.15);
 }

This will use the global scope variable instead.

2) You are never calling the functions, only defining them. Thus, you can invoke them like this:

FahrenheightConvert();        
KelvinConverter();

This will actually run the code you created. The final result is this:

        var celcius;
        var fahrenheight;
        var kelvin;

        celcius = Number(window.prompt("Enter Degree", 50));
        document.write(celcius + " degrees Celcius.");
        
        function FahrenheightConvert() {
           fahrenheight = Math.round(9 / 5 * (celcius + 32));
        }
        /* Actually call the funciton */
        FahrenheightConvert();
        
        document.write("<br>");
        document.write(fahrenheight + " degrees Fahrenheight.");
        
        function KelvinConverter(){
            kelvin = (celcius + 273.15);
        }
        /* Again, actually call the funciton */
        KelvinConverter();

        document.write("<br>");
        document.write(kelvin + " in Kelvin.");
P.S., since you're new to JavaScript, I will let you know of some another option you have, which you might find useful.

You also have to option of rewriting your function to return a value. Thus, your function will look like this (I've only used one for this example):

 function FahrenheightConvert(celcius) {
   var fahrenheight = Math.round(9 / 5 * (celcius + 32));
   return fahrenheight;
 }

This will make your call be equivalent to the value of fahrenheight. This way, you could change your write statement to:

document.write(FahrenheightConvert(celsius) + " degrees Fahrenheight.");

Finally, I should also mention that, as comments have mentioned, my first option (before the P.S.) is bad design and is not recommended. I would highly suggest rewriting your functions this way instead. That way, you have a general purpose conversion function you can use however you like, without worrying that it will write to the document directly, or something.

  • It’s really a bad practice to write functions like this that depend on and alter variables outside their scope. For simple functions like this, they should take an argument and return a result. – Mark Feb 14 '19 at 21:41
  • 1
    Agreed. However, that seems like what the OP was wanting, since they made some global vars and tried to use them. That says to me they expected the local var to get stuffed in the global--especially since the globals have no other purpose otherwise. I've updated my answer to suggest something better. –  Feb 14 '19 at 21:50
0

Your fahrenheight and kelvin variables are never written because the converters are inside functions which are never called. Since all your code is meant to run top to bottom, the simplest thing is just to do the calculations without defining functions.

var celcius;
        var fahrenheight;
        var kelvin;

        celcius = Number(window.prompt("Enter Degree", 50));
        document.write(celcius + " degrees Celcius.");
        
        var fahrenheight = Math.round(9 / 5 * (celcius + 32));
        document.write("<br>");
        document.write(fahrenheight + " degrees Fahrenheight.");
        
        var kelvin = (celcius + 273.15);
        document.write("<br>");
        document.write(kelvin + " in Kelvin.");
nvioli
  • 3,854
  • 3
  • 16
  • 33