Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Convert Celsius to Fahrenheit

To test your learning, you will create a solution "from scratch". Place your code between the indicated lines and it will be tested against multiple test cases.

The algorithm to convert from Celsius to Fahrenheit is the temperature in Celsius times 9/5, plus 32.

You are given a variable celsius representing a temperature in Celsius. Use the variable fahrenheit already defined and apply the algorithm to assign it the corresponding temperature in Fahrenheit.

Note:

Don't worry too much about the function and return statements as they will be covered in future challenges. For now, only use operators that you have already learned.

The code at the start of the challenge:

function convertToF(celsius) {
  var fahrenheit;
  // Only change code below this line
  
  
  // Only change code above this line
  return fahrenheit;
}

// Change the inputs below to test your code
convertToF(30);

There is the function declaration of function convertToF(celsius) {

.. You have a function name of convertToF and a parameter of celsius

Parameters are used just like variables. So, inside of the function you can use celsius JUST like any other variable.

If I have this code:

function addThree (num) {
    var result;
    result = num + 3;
    return result;
}

So, when we make the function call of:

addThree(10);

You're calling the function addThree You're also passing a value 10 as an argument.

In the function declaration of function addThree (num) { You see that there is a parameter defined of num

When we do addThree(10) then the value of the parameter num is passed the argument value of ... 10

Then if you follow the code through.. result = num + 3; ... and we know the value of num is 10.

Therefore, if we follow through the function we end up with ... result = 10 + 3; then result = 13; then we return the result.

If you then make another function call..

addThree(39);

You can use the same function to follow the operation:

When we do addThree(39) then the value of the parameter num is passed the argument value of ... 39

Then if you follow the code through.. result = num + 3; ... and we know the value of num is now 39.

Therefore, if we follow through the function we end up with ... result = 39 + 3; then result = 42; then we return the result.

If you make the function call of addThree(21) the value of num inside the function is 21.

If you make the function call of addThree(1000) the value of num inside the function is 1000.

If you make the function call of addThree(123456) the value of num inside the function is 123456.

function declaration of function convertToF(celsius) { } making the function call convertToF(55) the parameter celsius inside of the function has a value of 55

function declaration of function convertToF(celsius) { } making the function call convertToF(23) the parameter celsius inside of the function has a value of 23

The algorithm to convert from Celsius to Fahrenheit is the temperature in Celsius times 9/5, plus 32

@revisualize

This comment has been minimized.

Copy link
Owner Author

revisualize commented Jun 13, 2017

Spoiler Alert:
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Fahrenheit is the temperature in Celsius times 9/5, plus 32
Fahrenheit is Celsius times 9/5, plus 32
Fahrenheit is Celsius times 9/5 + 32
Fahrenheit is Celsius * 9/5 + 32
....

@Ellyria

This comment has been minimized.

Copy link

Ellyria commented Jun 16, 2017

This comment is in three parts.

Part 1:
I agree that parameters are a frequent source of confusion, and I like how you said, "Parameters are used just like variables. So, inside of the function you can use celsius JUST like any other variable."

It would be nice if the challenges somehow really hammered home that the parameters can have any name at all (within the naming convention rules), and that we often just happen to choose names that communicate to us what their purpose is:

  • for readability's sake, or...
  • to make the code easier for others to understand, or...
  • for us to understand if we walk away from it for a long while and come back to it again much later, or...
  • to help us to visualize what's happening to our data each step of the way as it passes through the function, or...
  • to aid in debugging, where we're already probably keeping track of a lot of information, and would have an easier time working with parameters that "speak" to us than ones named param1, param2, etc., making us have to figure out or remember what those do, or...
  • to help us to write code that conveys its purpose so well that comments aren't necessary.

That's all I can come up with for the moment, but I suspect there are many more reasons.

The biggest take-away from all of that would be that all a parameter can ever be is a place-holder, that the names are just a way to keep track of them and not to give them some sort of value or power or ability, and that the data that "wears" each parameter as it passes through the function is what they represent.

Aside: I don't know why, but it just occurred to me that a little paper check or a little plastic debit card representing the money in your bank account as it passes through the function of paying your electric bill, etc., might be a great way to explain parameters.

Part 2:
One thing I would have liked to see is a comparison between this:

function convertToF(celsius) {

and this:

function convertToF(chocolate) {

with a demonstration showing that there's no difference between them, whatsoever, and that both of them give the same result. I suspect that would be a "lightbulb moment" for quite a few people.

Part 3:
There's one more lesson that should probably also happen, and that's how to handle it when you get fewer parameters than the arguments needed by the function, because it's rough to get that one dropped on you with no preparation. I realize that freeCodeCamp wants people to reach and grow and do some learning on their own, but that particular assignment has turned quite a few of us into pretzels after continuous hand-holding up to that point, and it comes as somewhat of a shock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.