w3resource logo


CoffeeScript exercises

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

Solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="//jashkenas.github.io/coffee-script/extras/coffee-script.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</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

Live Demo :

JS Bin

Post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program