w3resource

CoffeeScript: Convert temperatures to and from celsius, fahrenheit

CoffeeScript : Exercise-8 with Solution

Write a CoffeeScript program to convert temperatures to and from celsius, fahrenheit.

[ Formula : c/5 = f-32/9 [ where c = temperature in celsius and f = temperature in fahrenheit ]

Expected Output :
60°C is 140 °F 45°F is 7.222222222222222°C

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="//jashkenas.github.io/coffee-script/extras/coffee-script.js"></script>
  <meta charset="utf-8">
  <title>Convert temperatures to and from celsius, fahrenheit</title>
</head>
<body>

</body>
</html>

CoffeeScript Code :

cToF = (celsius) ->
  cTemp = celsius
  cToFahr = cTemp * 9 / 5 + 32
  message = cTemp + '°C is ' + cToFahr + ' °F.'
  console.log message
  return

fToC = (fahrenheit) ->
  fTemp = fahrenheit
  fToCel = (fTemp - 32) * 5 / 9
  message = fTemp + '°F is ' + fToCel + '°C.'
  console.log message
  return

cToF 60
fToC 45

Sample Output:

"60°C is 140 °F."
"45°F is 7.222222222222222°C."

Live Demo :

See the Pen coffeescript-exercise-8 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/coffeescript-exercises/coffeescript-exercise-8.php