Regroup JSON Array in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:31:59

345 Views

Suppose, we have a JSON array of objects like this −const arr = [    {       "id": "03868185",       "month_10": 6,    },    {       "id": "03870584",       "month_6": 2,    },    {       "id": "03870584",       "month_7": 5,    },    {       "id": "51295",       "month_1": 1,    },    {       "id": "51295",       "month_10": 1,    },    {       "id": "55468",       "month_11": 1,    } ];Here, we ... Read More

Merge Two Sorted Arrays in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:30:47

252 Views

We are required to write a JavaScript function that takes in two sorted array of numbers. The function should merge the two arrays together to form a resultant sorted array and return that array.For example −If the two arrays are −const arr1 = [2, 6, 6, 8, 9]; const arr2 = [1, 4, 5, 7];Then the output array should be −const output = [1, 2, 4, 6, 6, 7, 8, 9];ExampleThe code for this will be −const arr1 = [2, 6, 6, 8, 9]; const arr2 = [1, 4, 5, 7]; const mergeSortedArrays = (arr1 = [], arr2 = []) ... Read More

Merge Two Arrays with Objects in One in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:30:11

1K+ Views

Suppose, we have two arrays of objects like these −const arr1 = [    {name:'test', lastname: 'test', gender:'f'},    {name:'test1', lastname: 'test1', gender:'f'},    {name:'test2', lastname: 'test2', gender:'m'} ]; const arr2 = [    {name:'test21', lastname: 'test21', gender:'f'},    {name:'test1', lastname: 'test1', gender:'f'},    {name:'test2', lastname: 'test2', gender:'m'},    {name:'test22', lastname: 'test22', gender:'m'} ];These arrays do not have any repeating objects within (repeating on the basis of 'name' property) but there exist some objects with repeating names in the first and second objects.We are required to write a JavaScript function that takes two such arrays and returns a new array.The ... Read More

Generate Random Number Divisible by N in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:18:33

596 Views

We are required to write a JavaScript function that takes in a number as the only argument. The function should then return a random generated number which is always divisible by the number provided by the argument.ExampleThe code for this will be −const num = 21; // function that generates random numbers divisible by n with a default upper limit of 1000000 const specialRandom = (num = 1, limit = 1000000) => {    // getting a random number    const random = Math.random() * limit;    // rounding it off to be divisible by num    const res = ... Read More

Find Word Starting with Specific Letter in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:18:14

681 Views

We are required to write a JavaScript function that takes in an array of string literals as the first argument and a single string character as the second argument.Then our function should find and return the first array entry that starts with the character specified by the second argument.ExampleThe code for this will be −const names = ['Naman', 'Kartik', 'Anmol', 'Rajat', 'Keshav', 'Harsh', 'Suresh', 'Rahul']; const firstIndexOf = (arr = [], char = '') => {    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(el.substring(0, 1) === char){   ... Read More

Find Value in Binary Tree Using JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:17:08

292 Views

We are required to write a JavaScript function on the prototype object of a BinarySearchTree data type that takes in a value and finds whether or not that value is contained in the BST.ExampleThe code for this will be -// class for a single Node for BST class Node {    constructor(value) {       this.value = value;    } } // class for BST // contains function to insert node and search for existing nodes class BinarySearchTree {    constructor() {       this._root = null;    };    insert(value) {       let node = ... Read More

Finding the Least Common Multiple of a Range of Numbers in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:16:42

276 Views

We are required to write a JavaScript function that takes in an array of exactly two numbers specifying a range.The function should then calculate the least common multiple of all the numbers within that range and return the final result.ExampleThe code for this will be −const range = [8, 3]; const gcd = (a, b) => {    return !b ? a : gcd(b, a % b); } const lcm = (a, b) => {    return a * (b / gcd(a,b)); }; const rangeLCM = (arr = []) => {    if(arr[0] > arr[1]) (arr = [arr[1], arr[0]]);    for(let x = result = arr[0]; x

Calculating Josephus Permutations Efficiently in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:15:25

381 Views

This problem takes its name by arguably the most important event in the life of the ancient historian Josephus − according to his tale, he and his 40 soldiers were trapped in a cave by the Romans during a siege.Refusing to surrender to the enemy, they instead opted for mass suicide, with a twist − they formed a circle and proceeded to kill one man every three, until one last man was left (and that it was supposed to kill himself to end the act).Josephus and another man were the last two and, as we now know every detail of ... Read More

Split Comma and Semicolon Separated String into Two-Dimensional Array in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:15:12

2K+ Views

Let's say we have a variable “users” that contains the following string of text where each user is separated by a semicolon and each attribute of each users is separated by a comma −const users = 'Bob, 1234, Bob@example.com;Mark, 5678, Mark@example.com';We are required to write a JavaScript function that takes in one such string and splits this into a multidimensional array that looks like this −const arr = [    ['Bob', 1234, 'Bob@example.com'],    ['Mark', 5678, 'Mark@example.com'] ];ExampleThe code for this will be −const users = 'Bob, 1234, Bob@example.com;Mark, 5678, Mark@example.com'; const splitByPunctuations = (str = '') => {   ... Read More

Sum of Array Object Property Values in New Array of Objects in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:14:02

715 Views

Suppose we have an array of objects that contains the data about some students and their marks like this −const arr = [    { subject: 'Maths', marks: '40', noOfStudents: '5' },    { subject: 'Science', marks: '50', noOfStudents: '16' },    { subject: 'History', marks: '35', noOfStudents: '23' },    { subject: 'Science', marks: '65', noOfStudents: '2' },    { subject: 'Maths', marks: '30', noOfStudents: '12' },    { subject: 'History', marks: '55', noOfStudents: '20' }, ];We are required to write a JavaScript function that takes in one such array.The function should eliminate the redundant entries on the basis ... Read More

Advertisements