
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Temperature converter using JavaScript
We are required to write a JavaScript function that takes in a string representing a temperature either in Celsius or in Fahrenheit.
Like this −
"23F", "43C", "23F"
We are required to write a JavaScript function that takes in this string and converts the temperature from Celsius to Fahrenheit and Fahrenheit to Celsius.
Example
Following is the code −
const temp1 = '37C'; const temp2 = '100F'; const tempConverter = temp => { const degree = temp[temp.length-1]; let converted; if(degree === "C") { converted = (parseInt(temp) * 9 / 5 + 32).toFixed(2) + "F"; }else { converted = ((parseInt(temp) -32) * 5 / 9).toFixed(2) + "C"; }; return converted; }; console.log(tempConverter(temp1)); console.log(tempConverter(temp2));
Output
Following is the output in the console −
98.60F 37.78C
- Related Articles
- How to create a temperature converter with HTML and JavaScript?
- How to Create a Currency Converter in JavaScript?
- How to create infix to postfix converter in JavaScript?
- How to create a weight converter with HTML and JavaScript?
- How to create a length converter with HTML and JavaScript?
- How to create a speed converter with HTML and JavaScript?
- ASCII to hex and hex to ASCII converter class in JavaScript
- Difference between Converter and Inverter
- Difference Between Handbrake and Freemake Video Converter
- How to install Any Video Converter on Windows?
- Getting data from temperature and humidity sensor using Arduino
- Temperature
- How to write the temperature conversion table by using function?
- What is temperature?
- Thermometer Reading Temperature

Advertisements