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 460 of 840
How to use named arguments in JavaScript functions?
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 MoreJavaScript (+) sign concatenates instead of giving sum?
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 MoreMapping the letter of a string to an object of arrays - JavaScript
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 MoreFunction to create diamond shape given a value in JavaScript?
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 MoreTrying to get number for each character in string - JavaScript
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 MoreJavaScript Update specific index in a boolean matrix?
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 MoreHow do I subtract one week from this date in JavaScript?
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 MoreAdding a function for swapping cases to the prototype object of strings - JavaScript
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 MoreHow to replace leading zero with spaces - JavaScript
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 MoreConvert HH:MM:SS to seconds with JavaScript?
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