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
Articles by AmitDiwan
Page 482 of 840
Find n highest values in an object JavaScript
Let's say, we have an object that describes various qualities of a football player like this − const qualities = { defence: 82, attack: 92, heading: 91, pace: 96, dribbling: 88, tenacity: 97, vision: 91, passing: 95, shooting: 90 }; We wish to write a function that takes in such object and a number n (n ≤ no. of keys ...
Read MoreSumming numbers from a string - JavaScript
We are required to write a JavaScript function that takes in a string that contains some one-digit numbers in between and the function should return the sum of all the numbers present in the string. Let's say the following is our string with numbers: const str = 'gdf5jhhj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3'; Method 1: Using for Loop with Type Conversion This approach splits the string into characters and checks each one using type conversion: const str = 'gdf5jhhj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3'; const sumStringNum = str => { const strArr = str.split(""); let ...
Read MoreJavaScript - Writing a string function to replace the kth appearance of a character
Let's say, we are required to write a String.prototype function that takes in three arguments. First argument is string that should be searched for substrings Second argument is the string, the occurrence of which String to be removed Third argument is a Number say n, nth occurrence of substring to be removed from string. The function should return the new string if the removal of the subStr from the string was successful, otherwise it should return -1 in all cases. Example Following is the code − const str = 'jkdsttjkdsre'; const subStr ...
Read MoreHow to separate alphabets and numbers from an array using JavaScript
We have an array that contains some number literals and some string literals mixed, and we are required to write a sorting function that separates the two and the two types should also be sorted within. This approach uses a custom comparator function with JavaScript's sort() method to first separate numbers from strings, then sort each type individually. Using Custom Sort Comparator The code for this sorting function will be: const arr = [1, 5, 'fd', 6, 'as', 'a', 'cx', 43, 's', 51, 7]; const sorter = (a, b) => { ...
Read MoreNearest palindrome in JavaScript
We are required to write a function that takes in a number n and returns a palindromic number that is nearest to the number n. A palindrome reads the same forwards and backwards. For example: If the input number is 264, then the output should be 262 If the input number is 7834, then the output should be 7887 Approach The approach divides the number into two halves based on its length and creates a palindrome by mirroring the first half. For odd-length numbers, the middle digit remains unchanged. Example const ...
Read MoreHow do I display the content of a JavaScript object in a string format?
To display JavaScript object content as a string, you have several methods. The most common approach is using JSON.stringify() which converts objects into readable JSON format. Using JSON.stringify() The JSON.stringify() method converts JavaScript objects into JSON strings: Display JavaScript Object // Create a JavaScript object var ...
Read MoreSwitch case calculator in JavaScript
Let's say, we are required to write a JavaScript function that takes in a string like these to create a calculator − "4 add 6" "6 divide 7" "23 modulo 8" Basically, the idea is that the string will contain two numbers on either sides and a string representing the operation in the middle. The string in the middle can take one of these five values − "add", "divide", "multiply", "modulo", "subtract" Our job is to return the correct result based on the string Syntax switch (expression) { ...
Read MoreReplacing all special characters with their ASCII value in a string - JavaScript
We are required to write a JavaScript function that takes in a string that might contain some special characters. The function should return a new string with all special characters replaced with their corresponding ASCII value. Understanding Special Characters Special characters are symbols that are not letters, numbers, or spaces. Examples include !, @, #, $, etc. Each character has a unique ASCII code that can be retrieved using the charCodeAt() method. Example Following is the code: const str = 'Th!s !s @ str!ng th@t cont@!ns some special characters!!'; const specialToASCII = str ...
Read MoreHow to implement asynchronous loop in JavaScript?
Asynchronous loops in JavaScript allow you to perform time-delayed or async operations within loops without blocking the main thread. This is essential when working with APIs, file operations, or timed delays. What is an Asynchronous Loop? A regular loop executes all iterations instantly. An asynchronous loop waits for each iteration to complete before moving to the next, using await with async functions. Using async/await with for Loop Asynchronous Loop ...
Read MoreFind longest string in array (excluding spaces) JavaScript
We are required to write a function that accepts an array of string literals and returns the index of the longest string in the array. While calculating the length of strings we don't have to consider the length occupied by whitespaces. If two or more strings have the same longest length, we have to return the index of the first string that does so. We will iterate over the array, split each element by whitespace, join again and calculate the length. We'll track the maximum length and its index, updating when we find a longer string. Syntax ...
Read More