AmitDiwan has Published 10744 Articles

Replace all characters in a string except the ones that exist in an array JavaScript

AmitDiwan

AmitDiwan

Updated on 19-Aug-2020 07:16:29

377 Views

Let’s say, we have to write a function −replaceChar(str, arr, [char])Now, replace all characters of string str that are not present in array of strings arr with the optional argument char. If char is not provided, replace them with ‘*’.Let’s write the code for this function.The full code will be ... Read More

Sorting objects according to days name JavaScript

AmitDiwan

AmitDiwan

Updated on 19-Aug-2020 07:15:03

2K+ Views

Let’s say, we have an array of objects that contains data about the humidity over the seven days of a week. The data, however, sits randomly in the array right now. We are supposed to sort the array of objects according to the days like data for Monday comes first, ... Read More

Find duplicate element in a progression of first n terms JavaScript

AmitDiwan

AmitDiwan

Updated on 19-Aug-2020 07:13:15

129 Views

Let’s say, we are given an array of numbers that contains first n natural numbers, but one element appears twice in the array, so the total number of elements is n+1. Our job is to write a function that takes in the array and returns the number that appears twice ... Read More

Highest and lowest in an array JavaScript

AmitDiwan

AmitDiwan

Updated on 19-Aug-2020 07:11:58

414 Views

We are required to write a function that takes in an array of numbers and returns the difference between its highest and lowest number.At first, create an array −const arr = [23, 54, 65, 76, 87, 87, 431, -6, 22, 4, -454];Now, find maximum and minimum values with Math.max() and ... Read More

Merge two arrays with alternating Values in JavaScript

AmitDiwan

AmitDiwan

Updated on 19-Aug-2020 07:10:27

875 Views

Let’s say, we are required to write a function that takes in two arrays and returns a new array that contains values in alternating order from first and second array. Here, we will just loop over both the arrays simultaneously picking values from them one after the other and feed ... Read More

How can I convert an array to an object by splitting strings? JavaScript

AmitDiwan

AmitDiwan

Updated on 19-Aug-2020 07:09:08

862 Views

Let’s say, we have an array of strings in which each value each element has a dash (-), left to which we have our key and right to which we have our value. Our job is to split these strings and form an object out of this array.Here is the ... Read More

Completely removing duplicate items from an array in JavaScript

AmitDiwan

AmitDiwan

Updated on 19-Aug-2020 07:07:57

299 Views

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 ... Read More

Convert number to alphabet letter JavaScript

AmitDiwan

AmitDiwan

Updated on 19-Aug-2020 07:07:07

2K+ Views

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) = RAnd so on.The ASCII CodesASCII codes ... Read More

Get the property of the difference between two objects in JavaScript

AmitDiwan

AmitDiwan

Updated on 19-Aug-2020 07:05:48

4K+ Views

Let’s say, we are given two objects that have similar key value pairs with one or key having different values in both objects. Our job is to write a function that takes in the two objects as argument and returns the very first key it finds having different values. If ... Read More

How to make a list of partial sums using forEach JavaScript

AmitDiwan

AmitDiwan

Updated on 19-Aug-2020 07:04:11

292 Views

We have an array of numbers like this −const arr = [1, 1, 5, 2, -4, 6, 10];We are required to write a function that returns a new array, of the same size but with each element being the sum of all elements until that point.So, the output should look ... Read More

Advertisements