Javascript Articles

Page 74 of 534

Array filtering using first string letter in JavaScript

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

Suppose we have an array that contains names of some people like this: const arr = ['Amy', 'Dolly', 'Jason', 'Madison', 'Patricia']; console.log(arr); [ 'Amy', 'Dolly', 'Jason', 'Madison', 'Patricia' ] We are required to write a JavaScript function that takes in one such array as the first argument, and two lowercase alphabet characters as second and third arguments. Then, our function should filter the array to contain only those elements that start with alphabets that fall within the range specified by the second and third arguments. Therefore, if the second and third arguments ...

Read More

Number of vowels within an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 789 Views

We are required to write a JavaScript function that takes in an array of strings, (they may be a single character or greater than that). Our function should simply count all the vowels contained in the array. Example Let us write the code − const arr = ['Amy', 'Dolly', 'Jason', 'Madison', 'Patricia']; const countVowels = (arr = []) => { const legend = 'aeiou'; const isVowel = c => legend.includes(c.toLowerCase()); let count = 0; arr.forEach(el => { for(let ...

Read More

Comparing objects in JavaScript and return array of common keys having common values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 815 Views

We are required to write a JavaScript function that takes in two objects. The function should return an array of all those common keys that have common values across both objects. Example The code for this will be − const obj1 = { a: true, b: false, c: "foo" }; const obj2 = { a: false, b: false, c: "foo" }; const compareObjects = (obj1 = {}, obj2 = {}) => { const common = Object.keys(obj1).filter(key => { if(obj1[key] === obj2[key] && obj2.hasOwnProperty(key)){ ...

Read More

Dividing a floating number, rounding it to 2 decimals, and calculating the remainder in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 891 Views

When dividing a floating-point number, you often need to round the result to a specific number of decimal places and handle any remainder. This is useful for financial calculations, splitting costs, or distributing values evenly. Problem Suppose we have a floating-point number: 2.74 If we divide this number by 4, the result is 0.685. We want to divide this number by 4 but the result should be rounded to 2 decimals. Therefore, the result should be: 3 times 0.69 and a remainder of 0.67 Solution Using toFixed() The ...

Read More

Converting numbers to Indian currency using JavaScript

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

Converting numbers to Indian currency format is a common requirement in web applications. JavaScript provides a built-in method to format numbers according to different locales, including the Indian numbering system. Understanding Indian Currency Format Indian currency uses a unique grouping system where numbers are grouped by hundreds (lakhs and crores) rather than thousands. For example: 1000 → ₹1, 000.00 129943 → ₹1, 29, 943.00 76768798 → ₹7, 67, 68, 798.00 Using toLocaleString() Method The toLocaleString() method with Indian locale ('en-IN') automatically handles the currency formatting: const num1 = 1000; const num2 ...

Read More

Finding average in mixed data type array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 421 Views

Suppose, we have an array of mixed data types like this − const arr = [1, 2, 3, 4, 5, "4", "12", "2", 6, 7, "4", 3, "2"]; We are required to write a JavaScript function that takes in one such array and returns the average of all such elements that are a number or can be partially or fully converted to a number. The string "3454fdf", isn't included in the problem array, but if it wasn't there, we would have used the number 3454 as its contribution to average. Example The code ...

Read More

Function to add up all the natural numbers from 1 to num in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 325 Views

We are required to write a JavaScript function that takes in a number, say num. Then our function should return the sum of all the natural numbers between 1 and num, including 1 and num. For example, if num is − const num = 5; Then the output should be − const output = 15; because, 1+2+3+4+5 = 15 The Mathematical Formula We will use the efficient mathematical formula to solve this problem − Sum of all natural numbers up to n = ...

Read More

Check if an array is growing by the same margin in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 182 Views

We are required to write a JavaScript function that takes in an array of numbers. Our function should return true if the difference between all adjacent elements is the same positive number, false otherwise. Syntax function growingMarginally(arr) { // Check if array has consistent positive differences // Return true/false } Example: Basic Implementation The code for this will be − const arr = [4, 7, 10, 13, 16, 19, 22]; const growingMarginally = arr => { if(arr.length

Read More

Function to check two strings and return common words in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 885 Views

We are required to write a JavaScript function that takes in two strings as arguments. The function should then check the two strings for common substrings and prepare an array of those common parts. The function will find all possible substrings from the first string and check if they exist in the second string, returning an array of matches. Example const str1 = "IloveLinux"; const str2 = "weloveNodejs"; const findCommon = (str1 = '', str2 = '') => { const common = Object.create(null); let i, j, part; for (i = 0; i < str1.length - 1; i++) { for (j = i + 1; j

Read More

Converting numbers to base-7 representation in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 794 Views

In JavaScript, converting a number to base-7 representation involves repeatedly dividing by 7, similar to binary conversion but using 7 instead of 2. Base-7 uses digits 0-6. Understanding Base-7 Conversion To convert a decimal number to base-7, we divide the number by 7 repeatedly and collect the remainders. The remainders, read in reverse order, form the base-7 representation. Example Here's how to implement base-7 conversion in JavaScript: const base7 = (num = 0) => { let sign = num < 0 ? '-' : ''; num ...

Read More
Showing 731–740 of 5,340 articles
« Prev 1 72 73 74 75 76 534 Next »
Advertisements