AmitDiwan has Published 10744 Articles

Longest distance between 1s in binary JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 06:35:12

399 Views

We are required to write a JavaScript function that in a positive integer, say n. The function should find and return the longest distance between any two adjacent 1's in the binary representation of n.If there are no two adjacent 1's, then we have to return 0.Two 1's are adjacent ... Read More

Check if a value exists in an array and get the next value JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 06:33:42

2K+ Views

We are required to write a JavaScript function that takes in an array of literals as the first argument and a search string as the second argument.The function should the array for the that search string. If that string exists in the array, we should return its next element from ... Read More

Get all methods of any object JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 06:32:48

1K+ Views

We are required to write a program (function) that takes in an object reference and returns an array of all the methods (member functions) that lives on that object.We are only required to return the methods in the array and not any other property that might have value of type ... Read More

How to unflatten a JavaScript object in a daisy-chain/dot notation into an object with nested objects and arrays?

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 06:31:15

547 Views

Suppose, we have an object like this −const obj = {    "firstName": "John",    "lastName": "Green",    "car.make": "Honda",    "car.model": "Civic",    "car.revisions.0.miles": 10150,    "car.revisions.0.code": "REV01",    "car.revisions.0.changes": "",    "car.revisions.1.miles": 20021,     "car.revisions.1.code": "REV02",    "car.revisions.1.changes.0.type":    "asthetic",    "car.revisions.1.changes.0.desc":    "Left tire cap",   ... Read More

Find the least duplicate items in an array JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 06:26:11

381 Views

We are required to write a JavaScript function that takes in an array of literals which may contain some duplicate values.The function should return an array of all those elements that are repeated for the least number of times.For example− If the input array is −const arr = [1, 1, ... Read More

Transforming array to object JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 06:24:35

158 Views

Suppose we have an array of strings like this −const arr = [ 'type=A', 'day=45' ];We are required to write a JavaScript function that takes in one such array. The function should construct an object based on this array. The object should contain a key/value pair for each string in ... Read More

Finding the majority element of an array JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 06:23:38

829 Views

We are given an array of size n, and we are required to find the majority element. The majority element is the element that appears more than [ n/2 ] times.Exampleconst arr = [2, 4, 2, 2, 2, 4, 6, 2, 5, 2]; const majorityElement = (arr = []) => ... Read More

Finding sum of a range in an array JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 06:22:36

321 Views

We are required to write an Array function (functions that lives on Array.prototype object). The function should take in a start index and an end index and it should sum all the elements from start index to end index in the array (including both start and end)Exampleconst arr = [1, ... Read More

Dynamic Programming: Is second string subsequence of first JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 06:21:11

1K+ Views

We are given two strings str1 and str2, we are required to write a function that checks if str1 is a subsequence of str2.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing ... Read More

Sort array according to the date property of the objects JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Nov-2020 06:19:37

160 Views

We are required to write a JavaScript function that takes in an array of objects of dates like this −const arr = [    {date: "2016-06-08 18:10:00"},    {date: "2016-04-26 20:01:00"},    {date: "2017-02-06 14:38:00"},    {date: "2017-01-18 17:30:21"},    {date: "2017-01-18 17:24:00"} ];We are required to write a JavaScript ... Read More

Advertisements