AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 389 of 840

Implementing custom function like String.prototype.split() function in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

We need to create a custom function that behaves like the built-in String.prototype.split() method. This function will split a string into an array based on a separator character or string. Problem Statement We are required to write a JavaScript function that extends the String prototype. The function should take a string separator as an argument and return an array of parts where the original string is split by that separator. Implementation Here's how to implement a custom split function: String.prototype.customSplit = function(sep = '') { const res = []; ...

Read More

Display the result difference while using ceil() in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 192 Views

The Math.ceil() function rounds a number UP to the nearest integer. While division might give decimal results, Math.ceil() ensures you get the next whole number. How Math.ceil() Works If your value is 4.5, 4.3, or even 4.1, Math.ceil() will return 5. It always rounds UP, unlike regular rounding. Example: Division Without ceil() Let's see normal division first: var result1 = 98; var result2 = 5; console.log("The actual result is=" + result1 / result2); The actual result is=19.6 Example: Division With ceil() Now let's apply Math.ceil() to round ...

Read More

Alternate casing a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

We are required to write a JavaScript function that takes in a string and constructs a new string with all the uppercase characters from the original string converted to lowercase and all the lowercase characters converted to uppercase from the original string. For example: If the string is − const str = 'The Case OF tHis StrinG Will Be FLiPped'; Expected Output Then the output should be − const output = 'tHE cASE of ThIS sTRINg wILL bE flIpPED'; Using Manual Character Code Manipulation This approach manually checks character ...

Read More

Transforming array to object JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 217 Views

Suppose we have an array of strings like this − const arr = [ 'type=A', 'day=45' ]; We are required to write a JavaScript function that takes in one such array. The function should construct an object based on this array. The object should contain a key/value pair for each string in the array. For any string, the part before '=' becomes the key and the part after it becomes the value. Method 1: Using for Loop const arr = [ 'type=A', 'day=45' ]; const arrayToObject = (arr = []) => ...

Read More

Dash separated cartesian product of any number of arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 230 Views

We need to write a JavaScript function that takes any number of arrays and computes their cartesian product, with elements separated by dashes. The cartesian product combines every element from the first array with every element from the second array, and so on. What is Cartesian Product? The cartesian product of two or more sets is the set of all possible combinations where we pick one element from each set. For example, if we have arrays ['a', 'b'] and ['1', '2'], the cartesian product would be ['a-1', 'a-2', 'b-1', 'b-2']. Implementation Here's how we can implement ...

Read More

Finding sum of sequence upto a specified accuracy using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 223 Views

We need to find the sum of a sequence where each term is the reciprocal of factorials. The sequence is: 1/1, 1/2, 1/6, 1/24, ... where the nth term is 1/n!. Understanding the Sequence The sequence can be written as: 1st term: 1/1! = 1/1 = 1 2nd term: 1/2! = 1/2 = 0.5 3rd term: 1/3! = 1/6 ≈ 0.167 4th term: 1/4! = 1/24 ≈ 0.042 Each term is 1 divided by the factorial of its position number. Mathematical Formula Sum = 1/1! + 1/2! + 1/3! + ... + 1/n! Implementation const num = 5; const seriesSum = (n = 1) => { let sum = 0; let factorial = 1; for (let i = 1; i

Read More

Number prime test in JavaScript by creating a custom function?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 220 Views

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In JavaScript, we can create a custom function to check if a number is prime. Basic Prime Number Function Here's a simple approach to check if a number is prime: function checkNumberIsPrime(number) { if (number

Read More

Algorithm for matrix multiplication in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

Matrix multiplication is a fundamental operation in linear algebra where two matrices are multiplied to produce a third matrix. In JavaScript, we can implement this algorithm using nested loops to calculate the dot product of rows and columns. Matrix Multiplication Rules For matrix multiplication to be valid, the number of columns in the first matrix must equal the number of rows in the second matrix. If matrix A is m×n and matrix B is n×p, the result will be an m×p matrix. Matrix A (m×n) ...

Read More

What is the "get" keyword before a function in a class - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 510 Views

The get keyword in JavaScript creates a getter method that allows you to access a function like a property. When you call the getter, it executes the function and returns its value without using parentheses. Syntax class ClassName { get propertyName() { // return some value } } Basic Example Here's how to define and use a getter method: class Employee { constructor(name) { ...

Read More

Find the least duplicate items in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 481 Views

We are required to write a JavaScript function that takes in an array of literals which may contain some duplicate values. The function should return an array of all those elements that are repeated for the least number of times. For example− If the input array is − const arr = [1, 1, 2, 2, 3, 3, 3]; Then the output should be − const output = [1, 2]; because 1 and 2 are repeated for the least number of times (2) Example const arr = [1, ...

Read More
Showing 3881–3890 of 8,392 articles
« Prev 1 387 388 389 390 391 840 Next »
Advertisements