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
Web Development Articles
Page 184 of 801
Checking digit sum of smallest number in the array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument. The function should first pick the smallest number from the array and then calculate the sum of all the digits of the number. If the digit sum of that number is even, we should return true, false otherwise. Problem Example If the input array is: const arr = [12, 657, 23, 56, 34, 678, 42]; Then the output should be: const output = false; Because the smallest ...
Read MoreDifference between two times using Dayjs JavaScript library?
Day.js is a lightweight JavaScript library for date manipulation. The diff() method calculates the difference between two time instances and returns the result in the specified unit. Syntax dayjs().diff(date, unit) Parameters date - The date to compare with unit - The unit of measurement: 'milliseconds', 'seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years' Example: Basic Time Difference Day.js Time Difference ...
Read MoreFinding the maximum number using at most one swap in JavaScript
We are required to write a JavaScript function that takes in a number as the first and the only argument. The task of our function is to perform at most one swap between any two digits of the number and yield the maximum possible number. If the number is already the maximum possible number, we should return the number itself. For example, if the input number is: const num = 1625; Then the output should be: const output = 6125; We swapped 1 and 6, and this is the only ...
Read MoreWhat is the best way to reduce and merge a collection of objects – JavaScript?
The best way to reduce and merge a collection of objects is to use Object.values() combined with reduce() to group objects by a key and merge their properties. This approach is useful when you have duplicate objects and want to consolidate them, such as combining student records with the same ID. Example Data Consider this collection of student objects with duplicates: var details = [ { studentId: 10, marks: 75, studentName: "John" }, { studentId: 10, marks: 75, studentName: "John" }, { studentId: ...
Read MoreFinding longest line of 1s in a matrix in JavaScript
Suppose, we have a binary matrix (an array of arrays that contains only 0 or 1) like this: const arr = [ [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 1] ]; We need to write a JavaScript function that takes a binary matrix as input and finds the longest line of consecutive ones. The line can be horizontal, vertical, diagonal, or anti-diagonal. For the above array, the output should be 3 because the longest line starts from arr[0][1] and spans diagonally to ...
Read MoreHow to convert a string with zeros to number in JavaScript?
When you have a string containing numbers with dots or zeros as separators, you can convert it to a number using JavaScript's built-in methods. This is commonly needed when processing formatted numeric data. The Problem Consider a string like "453.000.00.00.0000" where dots are used as thousand separators rather than decimal points. Direct conversion methods like Number() won't work correctly because JavaScript interprets the first dot as a decimal separator. let stringValue = "453.000.00.00.0000"; console.log("Original string:", stringValue); console.log("Direct Number() conversion:", Number(stringValue)); // NaN Original string: 453.000.00.00.0000 Direct Number() conversion: NaN Using parseInt() ...
Read MoreFinding the sum of two numbers without using '+', '-', '/', '*' in JavaScript
We are required to write a JavaScript function add() that takes in two numbers m and n. The function should, without using the four basic arithmetic operations add the two numbers taken as input and return the sum. The Challenge Without using +, -, *, or /, we need to implement addition using bitwise operations. This approach mimics how computers perform addition at the hardware level using binary logic. Example The code for this will be − const m = 67, n = 33; const add = (x, y) => { while(y !== 0){ let carry = x & y; x = x ^ y; y = carry
Read MoreHow to make filter and word replacement method - JavaScript?
In JavaScript, there's no built-in method to replace all occurrences of a word in a string. You can create custom functions using methods like split() and join(), or use modern approaches like replaceAll(). Let's explore different methods to filter and replace words in a string: var sentence = "Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript"; Method 1: Using split() and join() This method splits the string by the target word and joins the parts with the replacement: var sentence = "Yes, My Name ...
Read MoreFinding the longest substring uncommon in array in JavaScript
In JavaScript, finding the longest uncommon subsequence in an array involves identifying the longest string that appears only once or doesn't exist as a subsequence in other strings. Understanding Subsequence A subsequence is a sequence derived from another sequence by deleting some characters without changing the order of remaining elements. Any string is a subsequence of itself, and an empty string is a subsequence of any string. Problem Definition We need to find the length of the longest uncommon subsequence among an array of strings. An uncommon subsequence is a subsequence of one string that is ...
Read MoreHow to get only first word of object's value – JavaScript?
When working with objects that contain string values with multiple words, you often need to extract only the first word. The most effective approach is using the split() method with a space delimiter. Sample Data Let's work with an employee object containing names and technologies: const employeeDetails = [ { employeeName: "John Smith", employeeTechnology: "JavaScript HTML" }, { employeeName: "David Miller", employeeTechnology: "Java ...
Read More