w3resource

CoffeeScript Exercises, Practice, Solution

CoffeeScript Exercises [20 exercises with solution]

The best way we learn anything is by practice and exercise questions. We have started this section for those (beginner to intermediate) who are familiar with CoffeeScript/CoffeeScript. Hope, these exercises help you to improve your CoffeeScript coding skills. Happy Coding!

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

1. Write a CoffeeScript program to display the current day and time in the following format.
Sample Output : Today is : Friday.
Current time is : 4 PM : 50 : 22
Click me to see the solution

2. Write a CoffeeScript program to get the current date.
Expected Output :
mm-dd-yyyy, mm/dd/yyyy or dd-mm-yyyy, dd/mm/yyyy
Click me to see the solution

3. Write a CoffeeScript program to determine whether a given year is a leap year in the Gregorian calendar.
Click me to see the solution

4. Write a CoffeeScript program to find the area of a triangle where lengths of the three of its sides are 5, 6, 7.
Click me to see the solution

5. Write a CoffeeScript program to find 1st January be a Sunday between 2015 and 2050.
Click me to see the solution

6. Write a CoffeeScript program where the program takes a random integer between 1 to 10, the user is then prompted to input a guess number. If the user input matches with guess number, the program will display a message "Matched" otherwise display a message "Not matched".
Click me to see the solution

7. Write a CoffeeScript program to calculate days left until next Christmas.
Click me to see the solution

8. 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
Click me to see the solution

9. Write a CoffeeScript function that reverses a number.
Example x = 58973;
Expected Output : 37985
Click me to see the solution

10. Write a CoffeeScript function to check whether a passed string is a palindrome or not?
A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.
Click me to see the solution

11. Write a CoffeeScript function that returns a passed string with letters in alphabetical order.
Example string : 'webmaster'
Expected Output : 'abeemrstw'
Assume punctuation and numbers symbols are not included in the passed string.
Click me to see the solution

12. Write a CoffeeScript function that accepts a string as a parameter and converts the first letter of each word of the string in upper case.
Example string : 'the quick brown fox'
Expected Output : 'The Quick Brown Fox '
Click me to see the solution

13. Write a CoffeeScript function that accepts a string as a parameter and find the longest word within the string.
Example string : 'Web Development Tutorial'
Expected Output : 'Development'
Click me to see the solution

14. Write a CoffeeScript function that accepts a string as a parameter and counts the number of vowels within the string.
Note : As the letter 'y' can be regarded as both a vowel and a consonant, we do not count 'y' as a vowel here.
Example string : 'The quick brown fox'
Expected Output : 5
Click me to see the solution

15. Write a CoffeeScript function that accepts a number as a parameter and check the number is prime or not.
Note : A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
Click me to see the solution

16. Write a CoffeeScript function which will take an array of numbers stored and find the second lowest and second greatest numbers, respectively.
Sample array : [1,2,3,4,5]
Expected Output : 2,4
Click me to see the solution .

17. Write a CoffeeScript function to compute the factors of a positive integer.
Click me to see the solution

18. Write a CoffeeScript function to convert an amount to coins.
Sample function : amountTocoins(46, [25, 10, 5, 2, 1])
Here 46 is the amount. and 25, 10, 5, 2, 1 are coins.
Output : 25, 10, 10, 1
Click me to see the solution

19. Write a CoffeeScript function to compute the value of bn where n is the exponent and b is the bases. Accept b and n from the user and display the result.
Click me to see the solution

20. Write a CoffeeScript function to extract unique characters from a string.
Example string : "thequickbrownfoxjumpsoverthelazydog"
Expected Output : "thequickbrownfxjmpsvlazydg"
Click me to see the solution

Online Practice Editor:

See the Pen COFFEESCRIPT-BLANK-EDITOR by w3resource (@w3resource) on CodePen.


More to Come !

Note : Since JavaScript is a loosely-typed, dynamic and expressive language, you may accomplish the same task in various ways. Therefore the ways (solution of the exercises) described here are not the only ways to do stuff. Rather, it would be great, if this helps you anyway to choose your own methods.

[ Want to contribute to CoffeeScript exercises? Send your code (attached with a .zip file) to us at w3resource[at]yahoo[dot]com. Please avoid copyrighted materials.]



Follow us on Facebook and Twitter for latest update.