Need help converting celsius to fahrenheit! (SOLVED)

// WHAT I HAVE SO FAR

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

celsius *= 9/5;
celsius += 32;

// Only change code above this line
return fahrenheit;
}

// Change the inputs below to test your code
convertToF(-30);
convertToF(-10);
convertToF(0);
convertToF(20);
convertToF(30);

3 Likes

You are nearly there, actually. notice the lines below and above the comments telling you where to change the code? the function is looking for fahrenheit. so if you were to somehow assign the modified value of celsius to fahrenehit…

2 Likes

Would it be

fahrenheit=celsius

?

1 Like

Ah yes, thank you so much!

1 Like

Good job. You can also do it on a single line. like so:

function convertToF(celsius) {
  var fahrenheit;
  // Only change code below this line
  fahrenheit = (celsius * (9/5)) + 32; // fahenheit = celsius * 9/5 + 32 works too.
  // Only change code above this line
  return fahrenheit;
}

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

Perfect! I wasn’t aware you could use brackets like that

2 Likes

yup. and honestly, you don’t even need the brackets. JavaScript will do the order of operations correctly as I pointed out in the comment. I just like to use them for clarity’s sake.

And this…

fahrenheit = (celsius * 9/5) + 32;

…worked too.

So i guess algorithm isn’t too strict in Javascript? I haven’t yet comprehend how this will or can be used in future. I guess I will find out later will I?

Yup. Javascript follows standard order of operations, but even if you aren’t changing the order brackets can be helpful for human clarity. Regarding the usefulness of this challenge. As it’s stated in the instructions, it is mainly to help solidify the concepts you have been learning in the previous challenges. But you might want to keep the F to C formula in your back pocket for when you get to the local weather zipline. :wink:

2 Likes

Ah! Thanks for the tip :wink:

Hi I keep receiving error messages after adding this code:
function convertToF(celsius) {
var fahrenheit;
// Only change code below this line
farenheit = (celsius * (9/5)) + 32;

// Only change code above this line
return fahrenheit;
}

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

I keep getting these error messages:

convertToF(0) should return a number
convertToF(-30) should return a value of -22
convertToF(-10) should return a value of 14
convertToF(0) should return a value of 32
convertToF(20) should return a value of 68
convertToF(30) should return a value of 86

OOOPS I just realized my mistake. I spelled the word fahrenheit wrong!!! So sorry, I got it to work now! :slight_smile:

2 Likes

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

fahrenheit = celsius*9/5+32;
// Only change code above this line
return fahrenheit;
}

// Change the inputs below to test your code
convertToF(30);
convertToF(-10);
convertToF(0);
convertToF(20);

I’m lazy , so I tried the simplest way. hope it will help you guys.

Thank you all this post helped me alot!!

My working solution I did it this way

function convertToF(celsius) {
var fahrenheit;
// Only change code below this line
greatStuff = 9/5;
x = 32;

fahrenheit = celsius * greatStuff + 32;

// Only change code above this line
return fahrenheit;
}

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

Some how I got lost with the instructions. Thanks for your help! :slight_smile:

Based on the previous questions I really think their pushing for an answer like this:

function convertToF(celsius) {
  var fahrenheit;
  // Only change code below this line
  fahrenheit = celsius;
  fahrenheit *= 9;
  fahrenheit /= 5;
  fahrenheit += 32;
  
  // Only change code above this line
  return fahrenheit;
}

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

That being said, the math that others provided makes more sense from a traditional math perspective, and works too.

2 Likes

Yes, believe it or not the answer was SO simple :slight_smile: I am new to coding and this problem made me cry. Just keeping it real. But now that I se how simple it was, I now have anew set of eyes when looking at issues with code. Hope this helps!

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

fahrenheit = celsius*9/5+32;

// Only change code above this line
return fahrenheit;
}

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

same here, omg!!! i’ve put a console.log to verify my results and all were correct…
after i saw your post, i realized, that i missed the first “h” in fa–>h<–renheit

Thanks for the post it helped!