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 479 of 840
Insert a word before the file name's dot extension in JavaScript?
To insert a word before a file's extension, you can split the filename on the dot (.) and then reconstruct it with the new word. This technique is useful for creating versioned files, adding timestamps, or modifying filenames programmatically. Basic Example Let's start with a simple example where we insert "programming" before the ".js" extension: var actualJavaScriptFileName = "demo.js"; var addValueBetweenFileNameAndExtensions = "programming"; console.log("The actual File name = " + actualJavaScriptFileName); var [fileName, fileExtension] = actualJavaScriptFileName.split('.'); var modifiedFileName = `${fileName}-${addValueBetweenFileNameAndExtensions}.${fileExtension}`; console.log("After adding into the file name:"); console.log(modifiedFileName); The actual File ...
Read MoreFirst class function in JavaScript
JavaScript treats functions as first-class citizens, meaning they can be stored in variables, passed as arguments, and returned from other functions. This powerful feature enables functional programming patterns and higher-order functions. What are First-Class Functions? In JavaScript, functions are first-class objects, which means they have all the capabilities of other objects. They can be: Stored in variables and data structures Passed as arguments to other functions Returned as values from functions Assigned properties and methods Storing Functions in Variables First-Class Functions // Store function ...
Read MoreHow to calculate total time between a list of entries?
Let's say, we have an array that contains some data about the speed of a motor boat during upstreams and downstreams like this − Following is our sample array − const arr = [{ direction: 'upstream', velocity: 45 }, { direction: 'downstream', velocity: 15 }, { direction: 'downstream', velocity: 50 }, { direction: 'upstream', velocity: 35 }, { direction: 'downstream', velocity: 25 }, { ...
Read MoreJavaScript Adding array name property to JSON object [ES5] and display in Console?
In JavaScript, you can add a JSON object to an array and assign it a property name for better organization. This is useful when you need to convert object data into array format while maintaining structure. Initial Object Setup Let's start with a sample customer object: var customerDetails = { "customerFirstName": "David", "customerLastName": "Miller", "customerAge": 21, "customerCountryName": "US" }; Adding Object to Array Using push() Create a new array and use the push() method to add the ...
Read MoreHow to write a JavaScript function that returns true if a portion of string 1 can be rearranged to string 2?
We need to write a function that checks if the characters from one string can be rearranged to form another string. This is essentially checking if one string contains all the characters needed to build the second string. The function scramble(str1, str2) should return true if characters from str1 can be rearranged to match str2, otherwise false. Problem Examples str1 = 'cashwool', str2 = 'school' → true (cashwool contains: c, a, s, h, w, o, o, l - enough to make 'school') str1 = 'katas', str2 = 'steak' → false (katas missing 'e' ...
Read MoreJavaScript to check consecutive numbers in array?
To check for consecutive numbers in an array, you can use JavaScript's reduce() method or simpler approaches. This returns true for consecutive sequences like 100, 101, 102, and false otherwise. Method 1: Using reduce() with Objects This approach works with arrays of objects containing number properties: const sequenceIsConsecutive = (obj) => Boolean(obj.reduce((output, latest) => (output ? (Number(output.number) + 1 === Number(latest.number) ? latest : false) : false))); console.log("Is Consecutive = " + sequenceIsConsecutive([ ...
Read MoreGet the item that appears the most times in an array JavaScript
Let's say we are required to write a function that takes in an array of string/number literals and returns the index of the item that appears for the most number of times. We will iterate over the array and prepare a frequency map, and from that map we will return the index that makes the most appearances. Example const arr1 = [12, 5, 6, 76, 23, 12, 34, 5, 23, 34, 65, 34, 22, 67, 34]; const arr2 = [12, 5, 6, 76, 23, 12, 34, 5, 23, 34]; const mostAppearances = (arr) => { ...
Read MoreHow to format JSON string in JavaScript?
To format JSON string in JavaScript, use JSON.stringify() with spacing parameters. This method converts JavaScript objects to formatted JSON strings with proper indentation for better readability. Syntax JSON.stringify(value, replacer, space) Parameters value - The JavaScript object to convert replacer - Function or array to filter properties (use null for all properties) space - Number of spaces or string for indentation Example var details = { studentId: 101, studentFirstName: 'David', studentLastName: 'Miller', studentAge: 21, subjectDetails: { ...
Read MoreHow to slice an array with wrapping in JavaScript
Let's say, we are required to write an array method that overwrites the default Array.prototype.slice(). Usually the Array.prototype.slice() method takes in two arguments the start index and the end index, and returns a subarray of the original array from index start to end-1. What we wish to do is make this slice() function so it returns a subarray from index start to end and not end-1. We iterate over the array using a for loop which is faster than many array methods. Then return the required subarray, lastly we overwrite the Array.prototype.slice() with the method we just wrote. ...
Read MoreHow to use the map function to see if a time is in a certain time frame with JavaScript?
In JavaScript, you can use the map() function to iterate through schedule records and check if the current time falls within a specific time frame. This is useful for determining which activity or subject you should be working on at any given moment. Example Data Structure Let's start with a simple schedule containing subject names and their study times: const scheduleDetails = [ { subjectName: 'JavaScript', studyTime: '5 PM - 11 PM' }, { subjectName: 'MySQL', studyTime: '12 AM - 4 PM' } ] Using map() ...
Read More