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
Front End Technology Articles
Page 210 of 652
Breaking a string into chunks of defined length and removing spaces using JavaScript
We are required to write a JavaScript function that takes in a string sentence that might contain spaces as the first argument and a number as the second argument. Our function should first remove all the spaces from the string and then break the string into a number of chunks specified by the second argument. All the string chunks should have the same length with an exception of last chunk, which might, in some cases, have a different length. Solution Approach The solution involves two main steps: Remove all spaces using ...
Read MoreHow to get id from tr tag and display it in a new td with JavaScript?
When working with HTML tables, you often need to extract the ID from table rows and display them within the table itself. This tutorial shows how to get the ID from tags and add them as new table cells using JavaScript. Sample Table Structure Consider the following table with ID attributes on each row: StudentName StudentCountryName ...
Read MoreFinding the only out of sequence number from an array using JavaScript
Problem We are required to write a JavaScript function that takes in an array of numbers. The array is sorted in ascending order and only one element in the array is out of order. Our function should find and return that element. Approach The solution works by checking each element against its neighbors. When we find an element that is greater than the next element AND the next element is also greater than the element after it, we've found our out-of-sequence number. Example Following is the code: const arr = [1, 2, 3, 4, 17, 5, ...
Read MoreWhat's the most efficient way to turn all the keys of an object to lower case - JavaScript?
When working with JavaScript objects, you may need to convert all property keys to lowercase for consistency. Here's how to efficiently transform object keys using built-in JavaScript methods. Sample Object Let's start with an object that has uppercase keys: var details = { "STUDENTNAME": "John", "STUDENTAGE": 21, "STUDENTCOUNTRYNAME": "US" }; console.log("Original object:", details); Original object: { STUDENTNAME: 'John', STUDENTAGE: 21, STUDENTCOUNTRYNAME: 'US' } Method 1: Using Object.keys() and while Loop This approach uses a while loop to iterate ...
Read MoreFinding the sum of minimum value in each row of a 2-D array using JavaScript
Problem We are required to write a JavaScript function that takes in a 2-D array of numbers. Our function should pick the smallest number from each row of the 2-D array and then finally return the sum of those smallest numbers. Example Following is the code − const arr = [ [2, 5, 1, 6], [6, 8, 5, 8], [3, 6, 7, 5], [9, 11, 13, 12] ]; const sumSmallest = (arr = []) => { const findSmallest = array => ...
Read MoreMake JavaScript take HTML input from user, parse and display?
JavaScript can retrieve HTML input values as strings and parse them into different data types. This tutorial shows how to get user input from HTML forms and convert it appropriately. Basic Input Retrieval HTML input elements return string values by default. Use document.getElementById() to access input values: Input Parser Example Parse Input function parseInput() { ...
Read MoreGreatest number divisible by n within a bound in JavaScript
Problem We need to write a JavaScript function that takes in a divisor number n and a bound number b. Our function should find the largest integer num, such that: num is divisible by n (the divisor) num is less than or equal to b (the bound) num is greater than 0 Brute Force Approach The straightforward approach is to iterate through all multiples of n and find the largest one within the bound: const n = 14; const b = 400; const biggestDivisible = ...
Read MoreAlgorithm to sum ranges that lie within another separate range in JavaScript
We have two sets of ranges; one is a single range of any length (R1) and the other is a set of ranges (R2) some or parts of which may or may not lie within the single range (R1). We need to calculate the sum of the ranges in (R2) - whole or partial - that lie within the single range (R1). Problem Breakdown For each range in R2, we calculate the overlapping portion with R1 and sum all overlaps: const R1 = [20, 40]; const R2 = [[14, 22], [24, 27], [31, 35], [38, ...
Read MoreFinding the sum of all common elements within arrays using JavaScript
Problem We are required to write a JavaScript function that takes in three arrays of numbers. Our function should return the sum of all those numbers that are common in all three arrays. Example Following is the code − const arr1 = [4, 4, 5, 8, 3]; const arr2 = [7, 3, 7, 4, 1]; const arr3 = [11, 0, 7, 3, 4]; const sumCommon = (arr1 = [], arr2 = [], arr3 = []) => { let sum = 0; for(let i = 0; i ...
Read MoreHow to iterate an array of objects and build a new one in JavaScript ?
Suppose, we have an array of objects like this: const arr = [ { "customer": "Customer 1", "project": "1" }, { "customer": "Customer 2", "project": "2" }, { "customer": "Customer 2", ...
Read More