AmitDiwan has Published 10744 Articles

Replace a letter with its alphabet position JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:13:55

1K+ Views

We are required to write a function that takes in a string, trims it off any whitespaces, converts it to lowercase and returns an array of numbers describing corresponding characters positions in the english alphabets, any whitespace or special character within the string should be ignored.For example −Input → ‘Hello ... Read More

Find Max Slice Of Array | JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:12:36

219 Views

Let’s say, we are required to write a function that takes an array as input and returns the maximum slice of the array which contains no more than two different numbers. If we closely examine this problem this involves checking for a stable sub array and iterating over the original ... Read More

How to compare two string arrays, case insensitive and independent about ordering JavaScript, ES6

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:07:54

784 Views

We are required to write a function, say isEqual() that takes in two strings as argument and checks if they both contains the same characters independent of their order and case.For example −const first = 'Aavsg'; const second = 'VSAAg'; isEqual(first, second); //trueMethod: 1 Using arraysIn this method we convert ... Read More

Sort array based on presence of fields in objects JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:06:19

278 Views

Let’s say we have the following array of objects −const people = [{    firstName: 'Ram',    id: 301 }, {    firstName: 'Shyam',    lastName: 'Singh',    id: 1016 }, {    firstName: 'Dinesh',    lastName: 'Lamba',    id: 231 }, {    id: 341 }, {    firstName: ... Read More

Find all subarrays with sum equal to number? JavaScript (Sliding Window Algorithm)

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:01:40

602 Views

We are given an array of numbers and a number; our job is to write a function that returns an array of all the sub arrays which add up to the number provided as second argument.For example −const arr = [23, 5, 1, 34, 12, 67, 9, 31, 6, 7, ... Read More

Algorithm to dynamically populate JavaScript array with zeros before and after values

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 05:59:48

147 Views

We are given a months array, which elements less than 12, where each element will be between 1 and 12 (both inclusive). Our job is to take this array and create a full months array with 12 elements, if the element is present in the original array we use that ... Read More

Check if object contains all keys in JavaScript array

AmitDiwan

AmitDiwan

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

523 Views

We are required to write a function containsAll() that takes in two arguments, first an object and second an array of strings. It returns a boolean based on the fact whether or not the object contains all the properties that are mentioned as strings in the array.So, let’s write the ... Read More

How to get the product of two integers without using * JavaScript

AmitDiwan

AmitDiwan

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

320 Views

We are required to write a function that takes in two numbers and returns their product, but without using the (*) operator.Trick 1: Using Divide Operator TwiceWe know that multiplication and division are just the inverse of each other, so if we divide a number by other number’s inverse, won’t ... Read More

Remove number properties from an object JavaScript

AmitDiwan

AmitDiwan

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

272 Views

We are given an object that contains some random properties, including some numbers, boolean, strings and the object itself.We are required to write a function that takes in the object as first argument and a string as second argument, possible value for second argument is a name of any data ... Read More

Compute the sum of elements of an array which can be null or undefined JavaScript

AmitDiwan

AmitDiwan

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

781 Views

Let’s say, we have an array of arrays, each containing some numbers along with some undefined and null values. We are required to create a new array that contains the sum of each corresponding sub array elements as its element. And the values undefined and null should be computed as ... Read More

Advertisements