Math.min(.nums) //returns 2 //maximum number Here is the code example: let nums = //minimum number We can easily get the maximum and the minimum number from an array of numbers. Return the min and max number from an arrayīy using the methods Math.max() and Math.min() with the spread operator. : num * getFactorial(num - 1) //examples: Throw new TypeError('No negative numbers') Like you can see in the example below: const getFactorial = num => To calculate the factorial of a number, we will use an arrow function with the ternary operator. Then, we will use the method omCharCode() to convert decimals into text. To convert binary code into normal text using JavaScript, we will need to convert the binary into decimals first. Here is the code example: let str = "Hello World" str.split("").reverse().join("") //returns 'dlroW olleH' 4. That’s why we will convert the string to an array using split(), reverse it, and then convert it back to a string using the method join(). To easily reverse a string, we can use the method reverse(), but it only works for arrays. Return !isNaN(parseFloat(number)) & isFinite(number) In the below example, we will check if a function argument is a number by using the three methods isNaN(), parseFloat(), and isFinite(). Whenever you run the code again, a new random element from the array gets printed to the console. Let randomCar = cars //Print the random item from car array.Ĭonsole.log(randomCar) //returns random element Here is an example: let cars = //Store a random array item in a variable. To return a random item or element from an array, we will need to use the methods Math.random() and Math.floor() with the length property. That’s why in this article, I wanted to share with you some useful JavaScript code snippets to solve simple problems. However, sometimes you don’t need to always use frameworks and libraries for writing JavaScript code. The JavaScript ecosystem is full of frameworks and libraries that make life easier for the developer. ✍︎ Photo by Shamin Haky on UnsplashĪs we all know, JavaScript these days is one of the popular programming languages, especially when it comes to web development. Mehdi Aoussiad on JavaScript code snippets that you often will need to use. 7 Useful JavaScript Code Snippets for Solving Coding Problems 7 Useful JavaScript Code Snippets for Solving Coding Problems
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |