w3resource

CoffeeScript: Calculate number of days left until next Christmas

CoffeeScript : Exercise-7 with Solution

Write a CoffeeScript program to calculate days left until next Christmas.

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="//jashkenas.github.io/coffee-script/extras/coffee-script.js"></script>
  <meta charset="utf-8">
  <title>Calculate number of days left until next Christmas</title>
</head>
<body>

</body>
</html>

CoffeeScript Code :

today = new Date
cmas = new Date(today.getFullYear(), 11, 25)
if today.getMonth() == 11 and today.getDate() > 25
  cmas.setFullYear cmas.getFullYear() + 1
one_day = 1000 * 60 * 60 * 24
console.log Math.ceil((cmas.getTime() - today.getTime()) / one_day) + ' days left until Christmas!'

Sample Output:

"280 days left until Christmas!"

Live Demo :

See the Pen coffeescript-exercise-7 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://www.w3resource.com/coffeescript-exercises/coffeescript-exercise-7.php