Found 6710 Articles for Javascript

Convert an array of binary numbers to corresponding integer in JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:24:28

6K+ Views

Let’s say, we have an array of Numbers that contains 0 and 1 −const arr = [0, 1, 0, 1];We are required to write an Array function, toBinary() that returns the corresponding binary for the array it is used with.For example − If the array is −const arr = [1, 0, 1, 1];Then the output should be 11 because the decimal representation of binary 1011 is 11.Therefore, let’s write the code for this function.Method 1: Using library methodsIn JavaScript, there exists a method parseInt(), that takes in two arguments first one is a string and second a number that represents ... Read More

Return the maximum number in each array using map JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:22:55

736 Views

We have an array of arrays of Numbers like this one −const arr = [    [12, 56, 34, 88],    [65, 66, 32, 98],    [43, 87, 65, 43],    [32, 98, 76, 83],    [65, 89, 32, 54], ];We are required to write a function that maps over this array of arrays and returns an array that contains the maximum (greatest) element from each subarray.Therefore, let’s write the code for this function −Exampleconst arr = [    [12, 56, 34, 88],    [65, 66, 32, 98],    [43, 87, 65, 43],    [32, 98, 76, 83],    [65, 89, 32, 54], ]; const findMax = arr => {    return arr.map(sub => {       const max = Math.max(...sub);       return max;    }); }; console.log(findMax(arr));OutputThe output in the console will be −[    88,    98,    87,    98,    89 ]

Find consecutive elements average JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:21:55

195 Views

Let’s say, we have an array of numbers −const arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, 4, 2, 1];We are required to write a function that returns an array with the average of the corresponding element and its predecessor. For the first element, as there are no predecessors, so that very element should be returned.Let’s write the code for this function, we will use the Array.prototype.map() function to solve this problem −Exampleconst consecutiveAverage = arr => {    return arr.map((el, ind, array) => {       return ((el + (array[ind-1] || 0)) / (1 + !!ind));    }); }; console.log(consecutiveAverage(arr));OutputThe output in the console will be −[    3, 4, 6, 7.5, 5.5, 4,    6, 5.5, 3, 5, 6, 3,    1.5 ]

Extract properties from an object in JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:20:32

3K+ Views

We have to write a JavaScript function, say extract() that extracts properties from an object to another object and then deletes them from the original object.For example −If obj1 and obj2 are two objects, thenobj1 = {color:"red", age:"23", name:"cindy"} obj2 = extract(obj1, ["color", "name"])After passing through the extract function, they should become like −obj1 = { age:23 } obj2 = {color:"red", name:"cindy"}Therefore, let’s write the code for this function −Exampleconst obj = {    name: "Rahul",    job: "Software Engineer",    age: 23,    city: "Mumbai",    hobby: "Reading books" }; const extract = (obj, ...keys) => {    const ... Read More

Finding matches in two elements JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:19:21

207 Views

We are required to write a function that returns true if the string in the first element of the array contains all of the letters of the string in the second element of the array.For example, ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring their case.The arguments ["hello", "hey"] should return false because the string "hello" does not contain a "y".Lastly, ["Alien", "line"], should return true because all of the letters in "line" are present in "Alien".This is a fairly simple problem; we will just split the second ... Read More

Formatting dynamic json array JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:17:55

816 Views

Let’s say, we have an array of objects like this −const arr = [    {"name1": "firstString"},    {"name2": "secondString"},    {"name3": "thirdString"},    {"name4": "fourthString"},    {"name5": "fifthString"},    {"name6": "sixthString"}, ];We are required to write a function that takes one such array of objects and returns an object with all the properties listed in that object.So, let’s write the code for this function. It can be done through the Array reduce method −Exampleconst arr = [    {"name1": "firstString"},    {"name2": "secondString"},    {"name3": "thirdString"},    {"name4": "fourthString"},    {"name5": "fifthString"},    {"name6": "sixthString"}, ]; const reduceArray = ... Read More

How to remove every Nth element from an array JavaScript?

AmitDiwan
Updated on 26-Aug-2020 11:16:26

1K+ Views

Let’s say, we have to write a function remove Nth that takes in an array and a number n and it removes every nth element of the array in place.This can be done using the Array.prototype.splice() method and here is the code for doing so −Exampleconst arr = ['T', 'h', 'a', 'i', 's', 'b', ' ', 'i', 'c', 's', ' ', 'a', 't', 'h', 'e', 'e', ' ', 't', 's', 'o', 'r', 'n', 'g', 't', ' ', 't', 'n', 'h', 'a', 's', 't', ' ', 'o', 'n', 'e', 'o', 'v', 'e', 'a', 'r', ' ', 'f', 'e', 'n', 'a', 'd', ... Read More

JavaScript Return the lowest index at which a value should be inserted into an array once it has been sorted (either in ascending or descending order).

AmitDiwan
Updated on 26-Aug-2020 11:13:33

189 Views

We have to write a function that returns the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted (either in ascending or descending order). The returned value should be a number.For example, Let’s say, we have a function getIndexToInsert() −getIndexToInsert([1, 2, 3, 4], 1.5, ‘asc’) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).Likewise, getIndexToInsert([20, 3, 5], 19, ‘asc’) should return 2 because once the array has been sorted in ascending order it will look like [3, 5, 20] and ... Read More

Check if three consecutive elements in an array is identical in JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:10:32

818 Views

We are required to write a JavaScript function, say checkThree() that takes in an array and returns true if anywhere in the array there exists three consecutive elements that are identical (i.e., have the same value) otherwise it returns false.Therefore, let’s write the code for this function −Exampleconst arr = ["g", "z", "z", "v" ,"b", "b", "b"]; const checkThree = arr => {    const prev = {       element: null,       count: 0    };    for(let i = 0; i < arr.length; i++){       const { count, element } = prev;   ... Read More

What should be the correct Algorithm to Get Array B from Array A counting backwards from the last element in JavaScript?

AmitDiwan
Updated on 26-Aug-2020 11:08:10

94 Views

Consider the following binary array (Array A) −const arr = [1, 0, 1, 1, 1, 1, 0, 1, 1];When this array is passed through the function, say sumRight(), it produces the following output array (Array B) −const output = [1, 0, 4, 3, 2, 1, 0, 2, 1];Understanding the functionElements in array arr can be either 0 or 1. The function counts backward from the last element of array arr, if there are consecutive 1's in the array arr then the corresponding element in the output array will be 1 but for the 2nd consecutive 1 in array arr, it ... Read More

Advertisements