Javascript Articles

Page 335 of 534

Implementing Heap Sort using vanilla JavaScript

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 281 Views

Heap Sort is basically a comparison-based sorting algorithm. It can be thought of as an improved selection sort − like that algorithm, it divides its input into a sorted and an unsorted region, and it interactively shrinks the unsorted region by extracting the target (largest or smallest) element and moving that to the sorted region.ExampleThe code for this will be −const constructHeap = (arr, ind) => {    let left = 2 * ind + 1;    let right = 2 * ind + 2;    let max = ind;    if (left < len && arr[left] > arr[max]) { ...

Read More

String to binary in JavaScript

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 7K+ Views

We are required to write a JavaScript function that takes in a string as the only input. The function should construct and return the binary representation of the input string.For example −If the input string is −const str = 'Hello World';Then the output should be −const output = '1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100';ExampleThe code for this will be −const str = 'Hello World'; const textToBinary = (str = '') => {    let res = '';    res = str.split('').map(char => {       return char.charCodeAt(0).toString(2);    }).join(' ');    return res; }; console.log(textToBinary('Hello World'));OutputAnd the output in the console will be −1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100

Read More

Get the max n values from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 590 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n, as the second argument.Our function should then pick the n greatest numbers from the array and return a new array consisting of those numbers.ExampleThe code for this will be −const arr = [3, 4, 12, 1, 0, 5, 22, 20, 18, 30, 52]; const pickGreatest = (arr = [], num = 1) => {    if(num > arr.length){       return [];    };    const sorter = (a, b) => b - a;   ...

Read More

Remove all occurrences of a multiple occurring element in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 871 Views

We are required to write a JavaScript function that takes in an array of literal values.The array might contain some repeating values.Our function should remove all the values from the array that are repeating. We are required to remove all instances of all such elements.ExampleThe code for this will be −const arr = [1, 2, 3, 2, 4]; const removeAllInstances = (arr = []) => {    filtered = arr.filter(val => {       const lastIndex = arr.lastIndexOf(val);       const firstIndex = arr.indexOf(val);       return lastIndex === firstIndex;    });    return filtered; }; console.log(removeAllInstances(arr));OutputAnd the output in the console will be −[ 1, 3, 4 ]

Read More

Combine array of objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 395 Views

Suppose, we have an array of objects that contains data about some students like this −const arr = [{    name: 'A',    idNo: 1,    marks: {       math: 98,       sci: 97,       eng: 89    } }, {    name: 'B',    idNo: 2,    marks: {       math: 88,       sci: 87,       eng: 79    } }, {    name: 'C',    idNo: 3,    marks: {       math: 87,       sci: 98,       eng: 91   ...

Read More

Filter JavaScript array of objects with another array

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 622 Views

Suppose, we have an array of objects like this −const arr = [    {area: 'NY', name: 'Bla', ads: true},    {area: 'DF', name: 'SFS', ads: false},    {area: 'TT', name: 'SDSD', ads: true},    {area: 'SD', name: 'Engine', ads: false},    {area: 'NSK', name: 'Toyota', ads: false}, ];We are required to write a JavaScript function that takes in one such array as the first argument and an array of string literals as the second argument.Our function should then filter the input array of objects to contain only those objects whose "area" property is included in the array of literals ...

Read More

Remove the duplicate value from array with images data in JavaScript

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 486 Views

Suppose, we have some data regarding some images in an array like this −const arr = [{    'image': "jv2bcutaxrms4i_img.png",    'gallery_image': true }, {    'image': "abs.png",    'gallery_image': true }, {    'image': "acd.png",    'gallery_image': false }, {    'image': "jv2bcutaxrms4i_img.png",    'gallery_image': true }, {    'image': "abs.png",    'gallery_image': true }, {    'image': "acd.png",    'gallery_image': false }];We are required to write a JavaScript function that takes in one such array.Our function should remove the objects from the array that have duplicate values for the 'image' property.ExampleThe code for this will be −const arr ...

Read More

Grouping nested array in JavaScript

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 847 Views

Suppose, we have an array of values like this −const arr = [    {       value1:[1, 2],       value2:[{type:'A'}, {type:'B'}]    },    {       value1:[3, 5],       value2:[{type:'B'}, {type:'B'}]    } ];We are required to write a JavaScript function that takes in one such array. Our function should then prepare an array where the data is grouped according to the "type" property of the objects.Therefore, for the above array, the output should look like −const output = [    {type:'A', value: [1, 2]},    {type:'B', value: [3, 5]} ];ExampleThe code ...

Read More

JavaScript: create an array of JSON objects from linking two arrays

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 527 Views

Suppose, we have two arrays like these −const meals = ["breakfast", "lunch", "dinner"]; const ingredients = [    ["eggs", "yogurt", "toast"],    ["falafel", "mushrooms", "fries"],    ["pasta", "cheese"] ];We are required to write a JavaScript function that takes in two such arrays and maps the subarrays in the second array to the corresponding strings of the first array.Therefore, the output for the above arrays should look like −const output = {    "breakfast" : ["eggs", "yogurt", "toast"],    "lunch": ["falafel", "mushrooms", "fries"],    "dinner": ["pasta", "cheese"] };ExampleThe code for this will be −const meals = ["breakfast", "lunch", "dinner"]; const ingredients ...

Read More

Combining two arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 276 Views

We are required to write a JavaScript function that takes in two arrays of the same length.Our function should then combine corresponding elements of the arrays, to form the corresponding subarray of the output array, and then finally return the output array.If the two arrays are −const arr1 = ['a', 'b', 'c']; const arr2 = [1, 2, 3];Then the output should be −const output = [    ['a', 1],    ['b', 2],    ['c', 3] ];ExampleThe code for this will be −const arr1 = ['a', 'b', 'c']; const arr2 = [1, 2, 3]; const combineCorresponding = (arr1 = [], arr2 ...

Read More
Showing 3341–3350 of 5,338 articles
« Prev 1 333 334 335 336 337 534 Next »
Advertisements