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 99 of 589
Title Case A Sentence JavaScript
Let's say we are required to write a function that accepts a string and capitalizes the first letter of every word in that string and changes the case of all the remaining letters to lowercase. For example, if the input string is — hello world coding is very interesting The output should be — Hello World Coding Is Very Interesting Let's define a function capitaliseTitle() that takes in a string and capitalises the first letter of each word and returns the string — Using Split and Map Method ...
Read MoreWhen summing values from 2 arrays how can I cap the value in the new JavaScript array?
When working with arrays that represent RGB color values, you often need to add corresponding elements while ensuring the result doesn't exceed the maximum value of 255. This is a common requirement in graphics programming and color manipulation. Suppose we have two arrays, each containing three elements representing the red, green, and blue color values as integers. Our goal is to add the corresponding values to form a new RGB color array, capping any value that exceeds 255. Problem Statement We need to create a function that: Takes two arrays as input (representing RGB values) Adds ...
Read MoreHow to exclude certain values from randomly generated array JavaScript
We need to create a function that generates an array of random numbers while excluding certain values. The function takes two arguments: the desired array length and an array of numbers to exclude. Requirements: Generate random numbers between 0 and 100 Exclude numbers present in the exclusion array No duplicate numbers in the result Example const absentArray = [44, 65, 5, 34, 87, 42, 8, 76, 21, 33]; const len = 10; const generateRandom = (len, absentArray) => { const randomArray = []; for(let i ...
Read MoreHow to create a random number between a range JavaScript
Our job is to create a function, say createRandom, that takes in two arguments and returns a pseudorandom number between the range (max exclusive). Syntax const createRandom = (min, max) => { const diff = max - min; const random = Math.random(); return Math.floor((random * diff) + min); } Example const min = 3; const max = 9; const createRandom = (min, max) => { const diff = max - min; const random ...
Read MoreSort Array of numeric & alphabetical elements (Natural Sort) JavaScript
We have an array that contains some numbers and some strings. We are required to sort the array such that the numbers get sorted and placed before every string, and then the strings should be placed sorted alphabetically. For example, this array: const arr = [1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9]; console.log("Original array:", arr); Original array: [ 1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9 ] Should look like this after sorting: [1, 6, 7, 9, 47, 'afv', 'bdf', 'fdf', 'svd'] Implementation ...
Read MoreConverting Odd and Even-indexed characters in a string to uppercase/lowercase in JavaScript?
We need to write a function that reads a string and converts the odd indexed characters in the string to upperCase and the even ones to lowerCase and returns a new string. Understanding Index-Based Case Conversion In JavaScript, string indices start at 0. Even indices (0, 2, 4...) will be converted to lowercase, while odd indices (1, 3, 5...) will be converted to uppercase. Example const text = 'Hello world, it is so nice to be alive.'; const changeCase = (str) => { const newStr = str ...
Read MoreRecursive sum all the digits of a number JavaScript
Let's say, we are required to create a function that takes in a number and finds the sum of its digits recursively until the sum is a one-digit number. For example − findSum(12345) = 1+2+3+4+5 = 15 = 1+5 = 6 So, the output should be 6. How Recursive Digit Sum Works The process involves two levels of recursion: Extract and sum individual digits of a number Repeat the process until we get a single digit Method 1: Using Mathematical Operations ...
Read MoreHow to make a list of partial sums using forEach JavaScript
In JavaScript, creating a list of partial sums (also known as cumulative sums) means generating a new array where each element represents the sum of all elements up to that position in the original array. We have an array of numbers like this: const arr = [1, 1, 5, 2, -4, 6, 10]; We need to create a function that returns a new array where each element is the sum of all previous elements including itself: const output = [1, 2, 7, 9, 5, 11, 21]; Using forEach Method The ...
Read MoreConvert number to alphabet letter JavaScript
We are required to write a function that takes in a number between 1 and 26 (both inclusive) and returns the corresponding English alphabet for it. (capital case) If the number is out of this range return -1. For example: toAlpha(3) = C toAlpha(18) = R The ASCII Codes ASCII codes are the standard numerical representation of all the characters and numbers present on our keyboard and many more. The capital English alphabets are also mapped in the ASCII char codes, they start from 65 and go all the way up to 90, ...
Read MoreCompletely removing duplicate items from an array in JavaScript
We are required to write a function that takes in an array and returns a new array that have all duplicate values removed from it. The values that appeared more than once in the original array should not even appear for once in the new array. For example, if the input is: const arr = [23, 545, 43, 232, 32, 43, 23, 43]; The output should be: [545, 232, 32] Understanding indexOf() vs lastIndexOf() Array.prototype.indexOf() → Returns the index of first occurrence of searched ...
Read More