1. Converting Fahrenheit to Celsius
It is always good to be using a consistent unit of measurement of temperature, whether you choose to keep it as Fahrenheit or Celcius.
2. Random ID Generation
This can serve as an easy function to use for the time when you need prototyping and need to generate unique IDs.
3. Generate a Random Number
There are a lot of programs or scenarios where developers require the code to generate random numbers from within a given range. Math.random function is capable of achieving that and it can be transformed to get a range.
4. Shuffle An Array
5. Getting a Random Boolean
6. Generating a Random Hex Code
One-liners are often used as a challenge by developers. The one-liner shown below can be useful for generating a hexadecimal code at random. The code lime can generate up to 3 to 6 codes of colours that will help in creating a colour palette.
7. Reverse a String
8. Swapping Two Variables
9. Multiple Variable Assignment
10. Check If Even and Odd
A very basic code problem is determining whether a number is odd or even, a problem that every programmer faces in their journey. Many methods can be used for doing this, but a simple arrow function can be used to finish the task in just one line.
A string or a number that is the same when reversed is called a palindrome.
13. Checking Array Elements For A Condition
14. Calculating The Number Of Days
Many problems require the counting of days between two given dates. The first is to find the absolute between those two dates and then divide that number with 86400000. This will share the output of time in milliseconds in one day, and then the result is rounded and returned.
15. Converting A String To A Number
A very simple method of converting a string to number is to make use of type conversion.
16. Merging Multiple Arrays
A merge of more than 2 arrays is required in a lot of problems and can be done easily as shown below.
17. Truncate a number
To round a number to a fixed number of decimal places is necessary to achieve concise results. Math.pow() method is generally used for truncating the number to a required decimal point.
18. Scrolling To Top Of Page
The common method window.scrollTo() can be used to scroll the control of the page to the top. You will have to provide the x and y coordinate to the function, to which coordinate you want the control to go. If you set both the coordinates at zero, automatically it will scroll to the top.