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
Object Oriented Programming Articles
Page 14 of 589
Finding average in mixed data type array in JavaScript
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 MoreFunction to add up all the natural numbers from 1 to num in JavaScript
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 MoreCheck if an array is growing by the same margin in JavaScript
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 MoreFunction to check two strings and return common words in JavaScript
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 MoreConverting numbers to base-7 representation in JavaScript
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 MoreReduce an array to groups in JavaScript
In JavaScript, you can group consecutive duplicate elements in an array using the reduce() method. This technique combines adjacent duplicate values while preserving the original order. Problem Statement Given an array with duplicate entries, we need to merge consecutive duplicate elements together: const arr = ['blue', 'blue', 'green', 'blue', 'yellow', 'yellow', 'green']; console.log("Input array:", arr); Input array: [ 'blue', 'blue', 'green', 'blue', 'yellow', 'yellow', 'green' ] The expected output should combine only consecutive duplicates: [ 'blueblue', 'green', 'blue', 'yellowyellow', 'green' ] Using Array.reduce() Method The reduce() ...
Read MoreLoop through an index of an array to search for a certain letter in JavaScript
We need to write a JavaScript function that takes an array of strings and a single character, then returns true if the character exists in any string within the array, false otherwise. Problem Overview The function should search through each string in the array to find if the specified character exists anywhere. This is a common pattern for filtering or validation tasks. Example Implementation Here's how to implement this search functionality: const arr = ['first', 'second', 'third', 'last']; const searchForLetter = (arr = [], letter = '') => { ...
Read MoreReduce array in JavaScript
In JavaScript, arrays can be transformed using various methods. This article demonstrates how to extract and convert time values from an array of objects into a more usable format. Suppose we have an array of objects containing time strings: const arr = [ {"time":"18:00:00"}, {"time":"10:00:00"}, {"time":"16:30:00"} ]; We need to create a function that: Extracts the time strings from each object Converts times like "18:00:00" to arrays like [18, 0] Returns an array containing all converted time arrays Using map() to Transform ...
Read MoreJavaScript Reverse the order of the bits in a given integer
We are required to write a JavaScript program that reverses the order of the bits in a given integer. For example − 56 -> 111000 after reverse 7 -> 111 Another example, 234 -> 11101010 after reverse 87 -> 1010111 How It Works The algorithm converts the number to binary, reverses the binary string, and converts back to decimal: Convert number to binary string using toString(2) Split into array, reverse, and join back Parse reversed binary string to decimal using parseInt(reversedBinary, 2) Example const ...
Read MoreGo through an array and sum only numbers JavaScript
We are required to write a JavaScript function that takes in an array. The array might contain any type of value, Number literals, string literals, objects, undefined. Our function should pick all the Number type values and return their sum. Example const arr = [1, 2, 'a', 4]; const countNumbers = (arr = []) => { let sum = 0; for(let i = 0; i < arr.length; i++){ const el = arr[i]; ...
Read More