# JavaScript functions - Exercises, Practice, Solution

## JavaScript functions [29 exercises with solution]

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

**1. ** Write a JavaScript function that reverses a number.

Example x = 32243;

*Expected Output :* 34223

Click me to see the solution

**2. ** Write a JavaScript function that checks whether a passed string is a palindrome or not?

A palindrome is word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.

Click me to see the solution

**3. ** Write a JavaScript function that generates all combinations of a string.

*Example string :* 'dog'

*Expected Output :* d,do,dog,o,og,g

Click me to see the solution

**4. ** Write a JavaScript function that returns a string that has 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

**5. ** Write a JavaScript function that accepts a string as a parameter and converts the first letter of each word into upper case. ** **

*Example string *: 'the quick brown fox'

*Expected Output :* 'The Quick Brown Fox '

Click me to see the solution

**6. ** Write a JavaScript function that accepts a string as a parameter and finds the longest word within the string.

*Example string *: 'Web Development Tutorial'

*Expected Output :* 'Development'

Click me to see the solution

**7.** Write a JavaScript 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 vowel here.

*Example string *: 'The quick brown fox'

*Expected Output :* 5

Click me to see the solution

**8.** Write a JavaScript function that accepts a number as a parameter and checks whether it is prime or not using recursion.

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

**9. ** Write a JavaScript function that accepts an argument and returns the type.

Note : There are six possible values that typeof returns: object, boolean, function, number, string, and undefined.

Click me to see the solution

**10. ** Write a JavaScript function which returns the n rows by n columns identity matrix.

Click me to see the solution

**11. ** Write a JavaScript function that takes an array of numbers and finds 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.

**12. ** Write a JavaScript function that checks whether a number is perfect.

According to Wikipedia : In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum). Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).

*Example* : The first perfect number is 6, because 1, 2, and 3 are its proper positive divisors, and 1 + 2 + 3 = 6. Equivalently, the number 6 is equal to half the sum of all its positive divisors: ( 1 + 2 + 3 + 6 ) / 2 = 6. The next perfect number is 28 = 1 + 2 + 4 + 7 + 14. This is followed by the perfect numbers 496 and 8128.

Click me to see the solution.

**13. ** Write a JavaScript function to compute the factors of a positive integer.

Click me to see the solution.

**14. ** Write a JavaScript function to convert an amount into 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.

**15. ** Write a JavaScript function to compute the value of bn where n is the exponent and b is the base. Accept b and n from the user and display the results.

Click me to see the solution.

**16. ** Write a JavaScript function to extract unique characters from a string.

*Example string *: "thequickbrownfoxjumpsoverthelazydog"

*Expected Output *: "thequickbrownfxjmpsvlazydg"

Click me to see the solution.

**17. ** Write a JavaScript function to get the number of occurrences of each letter in a specified string.

Click me to see the solution.

**18. ** Write a function for searching JavaScript arrays with binary searches.

*Note* : A binary search searches by splitting an array into smaller and smaller chunks until it finds the desired value.

Click me to see the solution.

**19. ** Write a JavaScript function that returns array elements larger than a number.

Click me to see the solution.

**20. ** Write a JavaScript function that generates a string ID (specified length) of random characters.

*Sample character list* : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

Click me to see the solution.

**21. ** Write a JavaScript function to get all possible subsets with a fixed length (for example 2) combinations in an array.

*Sample array* : [1, 2, 3] and subset length is 2

*Expected output* : [[1,2],[1,3],[2,3]]

Click me to see the solution.

**22. ** Write a JavaScript function that accepts two arguments, a string and a letter and the function will count the number of occurrences of the specified letter within the string.

*Sample arguments * : 'w3resource.com', 'o'

*Expected output* : 2

Click me to see the solution

**23. ** Write a JavaScript function to find the first not repeated character.

*Sample arguments * : 'abacddbec'

*Expected output* : 'e'

Click me to see the solution

**24. ** Write a JavaScript function to apply the Bubble Sort algorithm.

*Note* : According to wikipedia "Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order".

*Sample array * : [12, 345, 4, 546, 122, 84, 98, 64, 9, 1, 3223, 455, 23, 234, 213]

*Expected output* : [3223, 546, 455, 345, 234, 213, 122, 98, 84, 64, 23, 12, 9, 4, 1]

Click me to see the solution

**25. ** Write a JavaScript function that accepts a list of country names as input and returns the longest country name as output.

*S**ample function * : Longest_Country_Name(["Australia", "Germany", "United States of America"])

*Expected output* : "United States of America"

Click me to see the solution

**26.** Write a JavaScript function to find the longest substring in a given string without repeating characters.

Click me to see the solution

**27. ** Write a JavaScript function that returns the longest palindrome in a given string.

Note: According to Wikipedia "In computer science, the longest palindromic substring or longest symmetric factor problem is
the problem of finding a maximum-length contiguous substring of a given string that is also a palindrome.
For example, the longest palindromic substring of "bananas" is "anana".
The longest palindromic substring is not guaranteed to be unique; for example,
in the string "abracadabra", there is no palindromic substring with length greater than three,
but there are two palindromic substrings with length three, namely, "aca" and "ada".

In some applications it may be necessary to return all maximal
palindromic substrings (that is, all substrings that are themselves palindromes and cannot
be extended to larger palindromic substrings) rather than returning only one substring or
returning the maximum length of a palindromic substring.

Click me to see the solution

**28.** Write a JavaScript program to pass a 'JavaScript function' as a parameter.

Click me to see the solution

**29.** Write a JavaScript function to get the function name.

Click me to see the solution

**More to Come !**

**Live Demo**

* To run the code mouse over on Result panel and click on 'RERUN' button.*

See the Pen javascript-common-editor by w3resource (@w3resource) on CodePen.

**Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.**

**Weekly Trends**- 100 Python Projects for Beginners with solution
- Mastering NumPy: 100 Exercises with solutions
- SQL Tutorial
- SQL Inner Join
- Python Exercises, Practice, Solution
- Python Interview Questions and Answers: Comprehensive Guide
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript HTML Form Validation
- Java Collection Exercises