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
Web Development Articles
Page 211 of 801
Returning an array containing last n even numbers from input array in JavaScript
Problem We are required to write a JavaScript function that takes in an array of numbers as the first argument and a number as the second argument. Our function should pick and return an array of last n even numbers present in the input array. Example Following is the code − const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const num = 3; const pickEvens = (arr = [], num = 1) => { const res = []; for(let index = ...
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 MoreConstructing an array of first n multiples of an input number in JavaScript
We are required to write a JavaScript function that takes in two numbers, let say m and n. Our function should construct and return an array of first n natural multiples of m. Problem Statement Given two numbers m and n, we need to create a function that returns an array containing the first n multiples of m. For example, if m = 6 and n = 5, the function should return [6, 12, 18, 24, 30]. Method 1: Using a For Loop The most straightforward approach is to use a for loop to iterate ...
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 MoreBreaking 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 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 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 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 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 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