Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Javascript Articles
Page 86 of 534
Reversing negative and positive numbers in JavaScript
Problem We are required to write a JavaScript function that takes in a number and returns its reversed number. One thing that we should keep in mind is that numbers should preserve their sign; i.e., a negative number should still be negative when reversed. Example Following is the code − const num = -224; function reverseNumber(n) { let x = Math.abs(n) let y = 0 while (x > 0) { y = y * 10 + (x ...
Read MoreReversing all alphabetic characters in JavaScript
We are required to write a JavaScript function that takes in a string and reverses only the alphabetic characters, omitting all non-alphabetic characters from the result. Problem Given a string containing both alphabetic and non-alphabetic characters, we need to create a function that filters out non-alphabetic characters and returns the alphabetic characters in reverse order. Example Following is the code − const str = 'exa13mple'; function reverseLetter(str) { const res = str.split('') .reverse() .filter(val ...
Read MoreDetermining sum of array as even or odd in JavaScript
Problem We are required to write a JavaScript function that takes in an array of integers arr. Our function should return the string 'odd' if the sum of all the elements of the array is odd or 'even' if it's even. Example Following is the code − const arr = [5, 1, 8, 4, 6, 9]; const assignSum = (arr = []) => { const sum = arr.reduce((acc, val) => { return acc + val; }, 0); ...
Read MoreCan all array elements mesh together in JavaScript?
Two words can mesh together if the ending substring of the first word is the starting substring of the second word. For instance, "robinhood" and "hoodie" can mesh together because "hood" appears at the end of "robinhood" and at the start of "hoodie". We need to write a JavaScript function that takes an array of strings and checks if all consecutive words mesh together. If they do, the function returns the meshed letters as a string, otherwise it returns an empty string. How It Works The solution uses a regular expression to find overlapping substrings between consecutive ...
Read MoreRearranging digits to form the greatest number using JavaScript
Problem We are required to write a JavaScript function that takes in one positive three-digit integer and rearranges its digits to get the maximum possible number. Approach To create the maximum number, we need to arrange the digits in descending order. We can convert the number to a string, split it into individual digits, sort them in descending order, and join them back. Example Following is the code − const num = 149; const maxRedigit = function(num) { if(num < 100 || num > 999) ...
Read MoreThird smallest number in an array using JavaScript
Problem We are required to write a JavaScript function that takes in an array of numbers of length at least three. Our function should simply return the third smallest number from the array. Example Following is the code − const arr = [6, 7, 3, 8, 2, 9, 4, 5]; const thirdSmallest = () => { const copy = arr.slice(); for(let i = 0; i < 2; i++){ const minIndex = copy.indexOf(Math.min(...copy)); copy.splice(minIndex, ...
Read MoreSending personalised messages to user using JavaScript
We need to write a JavaScript function that sends personalized messages based on whether the user is the owner or a regular user. The function takes two parameters: the user name and the owner name. Problem Statement Create a function that compares the user name with the owner name and returns appropriate greetings: If the user is the owner: return "Hello master" If the user is different from owner: return "Hello" followed by the user's name Solution Here's the implementation using a simple conditional statement: const name = 'arnav'; const owner ...
Read MoreNumbers and operands to words in JavaScript
We are required to write a JavaScript function that takes in a string of some mathematical operation and return its literal wording. Problem Converting mathematical expressions like "5 - 8" into readable words like "Five Minus Eight" requires mapping numbers and operators to their text equivalents. Approach We'll create two lookup objects: one for operators and another for numbers. Then parse the input string and convert each part using these mappings. Example Following is the code − const str = '5 - 8'; const convertToWords = (str = '') => { ...
Read MoreEncrypting censored words using JavaScript
This tutorial demonstrates how to encrypt or mask censored words in JavaScript by applying specific transformation rules to convert regular text into a masked format. Problem We need to write a JavaScript function that takes in a string and converts it according to the following rules: All words should be in uppercase Every word should end with '!!!!' Any letter 'a' or 'A' should become '@' Any other vowel (E, I, O, U) should become '*' Solution Here's the implementation of the word masking function: const str = 'ban censored words'; ...
Read MoreMoving every alphabet forward by 10 places in JavaScript
Problem We need to write a JavaScript function that takes a string of English alphabets and shifts every letter forward by 10 positions. When a letter goes past 'z', it wraps around to start again at 'a'. Example Following is the code − const str = 'sample string'; const moveStrBy = (num = 10) => { return str => { const calcStr = (ch, code) => String .fromCharCode(code + (ch.charCodeAt(0) - ...
Read More