Create a program that converts temperatures from Fahrenheit to celsius and vice versa

Hello! I am new to javascript and I am trying to create a program that converts temperatures from Fahrenheit to Celsius or from Celsius to Fahrenheit. I need to make a Prompt for the starting temperature. The program should prompt for the type of conversion and then perform the conversion.

I have made this but I am unsure how to use the user input from the “prompt” function in order to be used in the rest of the code.

var temp = prompt ( "What is the temperature?");
var conversion = prompt ( "Convert from C to F, or F to C");
if (conversion == "convert from C to F" ) { 
  console.log (( temp * 9/5) +32);
else if (conversion == "convert from F to C") { 
console.log (( temp - 32 ) * 5/9)

The prompt function asks for a string - it’s not multiple choice. Then you’d have to trust that they are going to enter the correct string.

If I were doing this, I would use an input element to get the numerical value and have some kind of radio button for the conversion direction. I guess you could still use the prompt for the number, but I find those kind of ugly.

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


Also I think this is a great operation to start thinking of in ‘reusable’ code. Or I’d restate/rewrite what you have written as a function that returns a temperature value.

This would also be a great exercise because a single function (say with at least two parameters), could do both types of conversion and still be reusable.