Found 6710 Articles for Javascript

Create a polyfill to replace nth occurrence of a string JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:43:56

503 Views

Let’s say, we have created a polyfill function removeStr() that takes in three arguments, namely −subStr → the occurrence of which String is to be removednum → it’s a Number (num)th occurrence of subStr is to be removed from stringThe function should return the new if the removal of the subStr from the string was successfully, otherwise it should return -1 in all cases.For example −const str = 'drsfgdrrtdr'; console.log(str.removeStr('dr', 3));Expected output −'drsfgdrrt'Let’s write the code for this −Exampleconst str = 'drsfgdrrtdr'; const subStr = 'dr'; const num = 2; removeStr = function(subStr, num){    if(!this.includes(subStr)){       return ... Read More

Finding out the Harshad number JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:42:30

575 Views

Harshad numbers are those numbers which are exactly divisible by the sum of their digits. Like the number 126, it is completely divisible by 1+2+6 = 9.All single digit numbers are harshad numbers.Harshad numbers often exist in consecutive clusters like [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [110, 111, 112], [1010, 1011, 1012].Our job is to write a function that takes in a Number as input checks whether it’s a harshad number or not, if not then returns -1 otherwise it returns the length of streak of consecutive harshad cluster.For example −harshadNum(1014) = harshadNum(1015) = harshadNum(1016) = ... Read More

How to create a random number between a range JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:39:28

249 Views

Our job is to create a function, say createRandom, that takes in two argument and returns a pseudorandom number between the range (max exclusive).The code for the function will be −Exampleconst min = 3; const max = 9; const createRandom = (min, max) => {    const diff = max - min;    const random = Math.random();    return Math.floor((random * diff) + min); } console.log(createRandom(min, max));Understanding the code −We take the difference of max and minWe create a random numberThen we multiply the diff and random to produce random number between 0 and diffThen we add min to it ... Read More

How to exclude certain values from randomly generated array JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:38:09

1K+ Views

We have to create a function that takes in 2 arguments: an integer and an array of integers. First argument denotes the length of array we have to return and the second argument contains the elements that should not be present in our return array. Actually, we need an array of random numbers between 0 to 100 but it should not include any element that’s present in the argument array.Note − No two numbers should be duplicate.Let’s call our function generateRandom(). The code for this would be −Exampleconst absentArray = [44, 65, 5, 34, 87, 42, 8, 76, 21, 33]; ... Read More

JavaScript - Converting array of objects into object of arrays

AmitDiwan
Updated on 28-Jan-2025 14:56:36

1K+ Views

In this article, we will learn to convert an array of objects into an object of arrays in JavaScript. When working with JavaScript, it's common to handle arrays of objects representing structured data. A frequent challenge is grouping these objects based on a shared property and transforming the result into a new data structure. Problem Statement The goal is to convert an array of objects into an object of arrays where the keys represent a specific property (e.g., roles) and the values are arrays of corresponding data (e.g., player names). Input const team = [ { role: ... Read More

JavaScript array.includes inside nested array returning false where as searched name is in array

AmitDiwan
Updated on 18-Aug-2020 07:43:39

1K+ Views

It is a well-known dilemma that when we use includes() inside nested arrays i.e., multidimensional array, it does not work, there exists a Array.prototype.flat() function which flats the array and then searches through but it’s browser support is not very good yet.So our job is to create a includesMultiDimension() function that takes in an array and a string and returns a boolean based on the presence/absence of that string in the array.There exist many solutions to this problem, most of them includes recursion, heavy array function, loops and what not.What we are going to discuss here is by far the ... Read More

When summing values from 2 arrays how can I cap the value in the new JavaScript array?

AmitDiwan
Updated on 18-Aug-2020 07:42:25

106 Views

Suppose, we have two arrays both containing three elements each, the corresponding values of red, green, blue color in integer.Our job is to add the corresponding value to form an array for new rgb color and also making sure if any value adds up to more than 255, we that value to 255.Therefore, let’s define a function addColors() that takes in two arguments, both arrays and returns a new array based on the input.The code for this will be −Exampleconst color1 = [45, 125, 216]; const color2 = [89, 180, 78]; const addColors = (color1, color2) => {    const newColor = color1.map((val, index) => {       return val + color2[index]

Title Case A Sentence JavaScript

AmitDiwan
Updated on 18-Aug-2020 07:41:22

290 Views

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 interestingThe output should be −Hello World Coding Is Very InterestingLet’s define a function capitaliseTitle() that takes in a string and capitalises the first letter of each word and returns the string −Examplelet str = 'hello world coding is very interesting'; const capitaliseTitle = (str) => {    const string = str    .toLowerCase()   ... Read More

How to find the one integer that appears an odd number of times in a JavaScript array?

AmitDiwan
Updated on 18-Aug-2020 07:40:26

186 Views

We are given an array of integers and told that all the elements appear for an even number of times except a single element. Our job is to find that element in single iteration.Let this be the sample array −[1, 4, 3, 4, 2, 3, 2, 7, 8, 8, 9, 7, 9]Before attempting this problem, we need to understand a little about the bitwise XOR (^) operator.The XOR operator returns TRUE if both the operands are complementary to each other and returns FALSE if both the operands are the same.TRUTH TABLE OF XOR () operator −0 ^ 0 → 0 ... Read More

Adding two arrays of objects with existing and repeated members of two JavaScript arrays replacing the repeated ones

AmitDiwan
Updated on 18-Aug-2020 07:39:37

147 Views

We have the following arrays of objects, we need to merge them into one removing the objects that have redundant value for the property name −const first = [{    name: 'Rahul',    age: 23 }, {    name: 'Ramesh',    age: 27 }, {    name: 'Vikram',    age: 35 }, {    name: 'Harsh',    age: 34 }, {    name: 'Vijay',    age: 21 }]; const second = [{    name: 'Vijay',    age: 21 }, {    name: 'Vikky',    age: 20 }, {    name: 'Joy',    age: 26 }, {    name: 'Vijay',   ... Read More

Advertisements