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
Front End Technology Articles
Page 377 of 652
Expressing numbers in expanded form - JavaScript
Suppose we are given a number 124 and are required to write a function that takes this number as input and returns its expanded form as a string. The expanded form of 124 is − '100+20+4' How It Works The algorithm converts each digit to its place value by multiplying it with the appropriate power of 10, then joins non-zero values with '+' signs. Example Following is the code − const num = 125; const expandedForm = num => { const numStr = String(num); ...
Read MoreJSON. stringify( ) function in JavaScript
The JSON.stringify() method converts a JavaScript object or value to a JSON string. This is the reverse of JSON.parse() which converts JSON strings back to JavaScript objects. Syntax JSON.stringify(value, replacer, space) Parameters value: The JavaScript value to convert to JSON string (required) replacer: Function or array to transform values (optional) space: Number of spaces or string for indentation (optional) Basic Example JSON.stringify Example var obj = {Tutorial: "JavaScript", Version: "ES6", ...
Read MoreChecking if two arrays can form a sequence - JavaScript
We are required to write a JavaScript function that takes in two arrays of numbers. And the function should return true if the two arrays upon combining and shuffling can form a consecutive sequence, false otherwise. For example − If the arrays are − const arr1 = [4, 6, 2, 9, 3]; const arr2 = [1, 5, 8, 7]; Then the output should be true because when combined and sorted, they form: [1, 2, 3, 4, 5, 6, 7, 8, 9] which is a consecutive sequence. Example Following is the code − ...
Read MoreWrite a program to find the index of particular element in an array in javascript?
Finding the index of a particular element in an array is a common task in JavaScript. An array is an object that contains multiple values in a sequential order, with each element having a specific index position. 22 45 67 89 12 0 1 2 3 4 Array Elements Index ...
Read MoreWrite a number array and using for loop add only even numbers in javascript?
In JavaScript, you can iterate through a number array and add only the even numbers using various loop methods. An even number is any integer that is divisible by 2, meaning when divided by 2, the remainder is 0. What are Even Numbers? Even numbers are integers that can be divided by 2 without leaving a remainder. We can check this using the modulo operator (%): if(number % 2 == 0) { // Number is even } Using for Loop The for loop is the most common way to iterate ...
Read MoreCount the number of data types in an array - JavaScript
We are required to write a JavaScript function that takes in an array that contains elements of different data types and the function should return a map representing the frequency of each data type. Let's say the following is our array: const arr = [23, 'df', undefined, null, 12, { name: 'Rajesh' }, [2, 4, 7], 'dfd', null, Symbol('*'), 8]; Understanding Data Types in JavaScript JavaScript's typeof operator returns string representations of data types. Note that arrays and null both return "object", which is a known quirk of JavaScript. Example ...
Read MoreHow to implement merge sort in JavaScript?
The Merge Sort algorithm is a divide-and-conquer sorting technique that recursively divides an array into smaller subarrays until each contains a single element, then merges them back in sorted order. The algorithm works by continuously splitting the array in half until it cannot be divided further. Once we have individual elements, we merge them back together in a sorted manner, comparing elements from each subarray and placing them in the correct order. Input-output Scenario Consider an unsorted array that we want to sort using merge sort: Input = [12, 34, 11, 1, 54, 25, 67, ...
Read MoreBeginning and end pairs in array - JavaScript
We are required to write a JavaScript function that takes in an array of Number / String literals and returns another array of arrays. With each subarray containing exactly two elements, the nth element from start and nth from last. For example, if we have an array: const arr = [1, 2, 3, 4, 5, 6]; Then the output should be: const output = [[1, 6], [2, 5], [3, 4]]; How It Works The algorithm pairs elements from both ends of the array moving inward: ...
Read MoreHow to Split large string in to n-size chunks in JavaScript?
A string is an order of one or more characters that contain numbers, letters, symbols, or special characters. In JavaScript strings are immutable. i.e., once you create a string you cannot change its value. For example, consider the following snippet here we have created a string variable and assigned a value (Tutorialspoint) to it. In the next statement, we are trying to change the contents of the string at the first index. Then we are displaying the contents of the string. let x = 'Tutorialspoint'; x[0] = 't'; console.log(x); // Tutorialspoint ...
Read MoreHow to remove non-word characters in JavaScript?
To remove non-word characters in JavaScript, we use regular expressions to replace unwanted characters with empty strings. Non-word characters include symbols, punctuation, and special characters that aren't letters, numbers, or underscores. What are Non-Word Characters? Non-word characters are anything that doesn't match the \w pattern in regex. This includes symbols like !@#$%^&*(), punctuation, and special characters, but excludes letters (a-z, A-Z), digits (0-9), and underscores (_). Using Simple Regex Pattern The most straightforward approach uses the \W pattern, which matches any non-word character: function removeNonWordChars(str) { if ...
Read More