Javascript Articles

Page 379 of 534

Grouping array nested value while comparing 2 objects - JavaScript

AmitDiwan
AmitDiwan
Updated on 01-Oct-2020 451 Views

Suppose, we have the following JSON object −const input = {    "before": {      "device": [        {          "id": "1234",          "price": "10",          "features": [            {              "name": "samsung",              "price": "10"            },            {              "name": "Apple",              "price": "20"            }         ...

Read More

Compare and fill arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 01-Oct-2020 261 Views

We are required to write a function that compares two arrays and creates a third array filling that array with all the elements of the second array and filling null for all those elements that are present in the first array but misses out in the second array.For example −If the two arrays are −const arr1 = ['f', 'g', 'h']; const arr2 = ['f', 'h'];Then the output should be −const output = ['f', null, 'h'];ExampleFollowing is the code −const arr1 = ['f', 'g', 'h']; const arr2 = ['f', 'h']; const compareAndFill = (arr1, arr2) => {    let offset = ...

Read More

How to validate if an element in an array is repeated? - JavaScript

AmitDiwan
AmitDiwan
Updated on 01-Oct-2020 336 Views

We are required to write a JavaScript function that takes in two arguments −An Array, say arr, of literals that may contain some repeating elements.A number, say limit.The function should validate that no element of the array is repeated more than limit number of times. If any element is repeated more than the limit the function should return false, true otherwise.ExampleFollowing is the code −const arr = [4, 6, 7, 4, 2, 5, 7, 7, 4, 4, 3]; const validateElements = (arr, n) => {    const counts = arr.reduce((acc, el) => {       acc[el] = (acc[el] + ...

Read More

Calculate Subtraction of diagonals-summations in a two-dimensional matrix using JavaScript

AmitDiwan
AmitDiwan
Updated on 01-Oct-2020 279 Views

Suppose, we have a square matrix represented by a 2-D array in JavaScript like this −const arr = [    [1, 3, 5],    [3, 5, 7],    [2, 4, 2] ];We are required to write a JavaScript function that takes in one such array.The function should return the difference between the sum of elements present at the diagonals of the matrix.Like for the above matrix, the calculations will be −|(1+5+2) - (5+5+2)| |8 - 12| 4ExampleFollowing is the code −const arr = [    [1, 3, 5],    [3, 5, 7],    [2, 4, 2] ]; const diagonalDiff = ...

Read More

Find closest index of array in JavaScript

AmitDiwan
AmitDiwan
Updated on 01-Oct-2020 1K+ Views

Suppose, we have an array like this −const arr = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362];We are required to write a JavaScript function that takes in one such array and a number, say n.The function should return the index of item from the array which is closest to the number n.ExampleFollowing is the code −const arr = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362]; const closestIndex = (num, arr) => {    let curr = arr[0], diff = Math.abs(num - curr);    let index = 0;    for (let val = 0; val ...

Read More

Repeat even number inside the same array - JavaScript

AmitDiwan
AmitDiwan
Updated on 01-Oct-2020 133 Views

We are required to write a JavaScript function that should repeat the even number inside the same array.Therefore, for example given the following array −const arr = [1, 2, 5, 6, 8];We should get the output −const output = [1, 2, 2, 5, 6, 6, 8, 8];ExampleFollowing is the code −const arr = [1, 2, 5, 6, 8]; const repeatEvenNumbers = arr => {    let end = arr.length -1;    for(let i = end; i > 0; i--){       if(arr[i] % 2 === 0){          arr.splice(i, 0, arr[i]);       };    };    return arr; }; console.log(repeatEvenNumbers(arr));OutputThis will produce the following output on console −[     1, 2, 2, 5,     6, 6, 8, 8 ]

Read More

Recursion problem Snail Trail in JavaScript

AmitDiwan
AmitDiwan
Updated on 01-Oct-2020 201 Views

Suppose, we have an array like this −const arr = [    [1, 2, 3, 4],    [12, 13, 14, 5],    [11, 16, 15, 6],    [10, 9, 8, 7] ];The array is bound to be a square matrix.We are required to write a JavaScript function that takes in this array and constructs a new array by taking elements and spiraling in until it converges to center. A snail trail spiraling around the outside of the matrix and inwards.Therefore, the output for the above array should be −const output = [1, 2, 3, 4, 5, 6, 7, 8, 9, ...

Read More

Capitalize letter in a string in order and create an array to store - JavaScript

AmitDiwan
AmitDiwan
Updated on 01-Oct-2020 142 Views

We are required to write a JavaScript function that takes in a string and turns it into a Mexican Wave i.e. resembling string produced by successive captial letters in every word −For example −If the string is −const str = 'edabit';Then the output should be the following i.e. successive single capital letter −const output = ["Edabit", "eDabit", "edAbit", "edaBit", "edabIt", "edabiT"];ExampleFollowing is the code −const str = 'edabit'; const replaceAt = function(index, char){    let a = this.split("");    a[index] = char;    return a.join(""); }; String.prototype.replaceAt = replaceAt; const createEdibet = word => {    let array = word.split('') ...

Read More

Sorting an array of objects by property values - JavaScript

AmitDiwan
AmitDiwan
Updated on 01-Oct-2020 431 Views

Suppose, we have an array of objects like this −const homes = [    {        "h_id": "3",        "city": "Dallas",        "state": "TX",        "zip": "75201",        "price": "162500"    }, {        "h_id": "4",        "city": "Bevery Hills",        "state": "CA",        "zip": "90210",        "price": "319250"    }, {        "h_id": "5",        "city": "New York",        "state": "NY",        "zip": "00010",        "price": "962500"    } ...

Read More

Finding the length of a JavaScript object

AmitDiwan
AmitDiwan
Updated on 01-Oct-2020 262 Views

Suppose we have an object like this −const obj = {    name: "Ramesh",    age: 34,    occupation: "HR Manager",    address: "Tilak Nagar, New Delhi",    experience: 13 };We are required to write a JavaScript function on Objects that computes their size (i.e., the number of properties in it).ExampleFollowing is the code −const obj = {    name: "Ramesh",    age: 34,    occupation: "HR Manager",    address: "Tilak Nagar, New Delhi",    experience: 13 }; Object.prototype.size = function(obj) {    let size = 0, key;    for (key in obj) {       if (obj.hasOwnProperty(key)){          size++       };    };    return size; }; const size = Object.size(obj); console.log(size);This will produce the following output on console −5

Read More
Showing 3781–3790 of 5,338 articles
« Prev 1 377 378 379 380 381 534 Next »
Advertisements