AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 460 of 840

How to use named arguments in JavaScript functions?

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

In this article, we will learn how to use named arguments in JavaScript, and how we can use it to significantly enhance code readability and maintainability. JavaScript allows us to simulate named arguments, which eliminate the need for the order of parameters to matter during function execution. We can then access the arguments by name inside the function definitions. Let's look at some of the examples and methods to understand the concept better: What are Named Arguments? Named arguments allow you to pass parameters to a function by explicitly specifying the parameter name rather than relying ...

Read More

JavaScript (+) sign concatenates instead of giving sum?

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

The + sign concatenates strings instead of adding numbers when JavaScript treats the values as strings. This commonly happens with form inputs, which always return string values even when they contain numbers. The Problem: String Concatenation vs Addition When you use the + operator with strings, JavaScript concatenates them instead of performing mathematical addition: String Concatenation Problem // These are strings, not numbers var a = ...

Read More

Mapping the letter of a string to an object of arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 503 Views

Given a string, we are required to write a function that creates an object that stores the indexes of each letter in an array. The letters (elements) of the string must be the keys of object. The indexes should be stored in an array and those arrays are values. For example, if the input string is: const str = 'cannot'; Then the output should be: const output = { 'c': [0], 'a': [1], 'n': [2, 3], ...

Read More

Function to create diamond shape given a value in JavaScript?

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

In JavaScript, you can create a function to generate diamond-shaped patterns using stars and spaces. A diamond shape consists of two parts: an upper triangle that expands and a lower triangle that contracts. Example function createDiamondShape(size) { // Upper part of diamond (including middle) for (var i = 1; i = i; s--) { process.stdout.write(" "); } // Print stars for (var j = 1; j

Read More

Trying to get number for each character in string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 268 Views

We are required to write a JavaScript function that takes in a string. It should print out each number for every corresponding letter in the string. For example, a = 1 b = 2 c = 3 d = 4 e = 5 . . . Y = 25 Z = 26 Therefore, if the input is "hello man", Then the output should be a number for each character − "8, 5, 12, 12, 15, 13, 1, 14" How It Works The solution uses the charCodeAt() method to get ...

Read More

JavaScript Update specific index in a boolean matrix?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 258 Views

To update a specific index in a boolean matrix in JavaScript, you can directly access the element using array indexing and assign a new boolean value. Let's explore different approaches to create and modify boolean matrices. Creating a Boolean Matrix First, let's create a boolean matrix using the fill() method: const array = Array(4); var fillWithTrueValue = array.fill(true); const matrixWithOnlyBooleanTrue = Array(4).fill(fillWithTrueValue); console.log("Original matrix:"); console.log(matrixWithOnlyBooleanTrue); Original matrix: [ [ true, true, true, true ], [ true, true, true, true ], [ true, true, true, true ], ...

Read More

How do I subtract one week from this date in JavaScript?

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

To subtract one week (7 days) from a date in JavaScript, use the setDate() method combined with getDate() to modify the date object directly. Syntax var newDate = new Date(currentDate.setDate(currentDate.getDate() - 7)); Method 1: Modifying Current Date First, get the current date, then subtract 7 days using setDate(): var currentDate = new Date(); console.log("The current Date = " + currentDate); var before7Daysdate = new Date(currentDate.setDate(currentDate.getDate() - 7)); console.log("The One week ago date = " + before7Daysdate); The current Date = Tue Jul 14 2020 19:12:43 GMT+0530 (India Standard ...

Read More

Adding a function for swapping cases to the prototype object of strings - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 199 Views

In JavaScript, we can extend built-in data types by adding custom methods to their prototype objects. This allows us to create reusable functions that work with existing data types like strings, arrays, and objects. In this tutorial, we'll create a custom swapCase() method for strings that swaps the case of each character - converting uppercase letters to lowercase and vice versa, while keeping non-alphabetic characters unchanged. Understanding String Prototype Extension When we add a method to String.prototype, it becomes available to all string instances. The this keyword inside the method refers to the string that called the ...

Read More

How to replace leading zero with spaces - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 382 Views

We are required to write a JavaScript function that takes in a string that represents a number. Replace the leading zero with spaces in the number. We need to make sure the prior spaces in number are retained. For example, If the string value is defined as − " 004590808" Then the output should come as − " 4590808" Example Following is the code − const str = ' 004590808'; const replaceWithSpace = str => { let replaced = ''; const regex = new RegExp(/^\s*0+/); ...

Read More

Convert HH:MM:SS to seconds with JavaScript?

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

To convert HH:MM:SS to seconds with JavaScript, we use simple arithmetic operations like multiplication and addition. We split the time string and convert each component to seconds using the formula: hours × 3600 + minutes × 60 + seconds. In this article, we are given a time in HH:MM:SS format and our task is to convert it to total seconds using JavaScript. Steps to Convert HH:MM:SS to Seconds Use the split() function to separate hours, minutes, and seconds by colon delimiter. Convert hours to seconds by multiplying by 3600 ...

Read More
Showing 4591–4600 of 8,392 articles
« Prev 1 458 459 460 461 462 840 Next »
Advertisements