AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 504 of 840

Finding word starting with specific letter in JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 711 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

How to find a value is present in binary tree or not in JavaScript ?

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 306 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
AmitDiwan
Updated on 21-Nov-2020 309 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

Read More

Calculating Josephus Permutations efficiently in JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 406 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

How to split comma and semicolon separated string into a two-dimensional array in JavaScript ?

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 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
AmitDiwan
Updated on 21-Nov-2020 751 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

How to round up to the nearest N in JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 2K+ Views

Suppose we have a number, const num = 76;However, If we round off this number to nearest 10 place, the result will be 80If we round off this number to nearest 100 place, the result will be 100If we round off this number to nearest 1000 place, the result will be 0We are required to write a JavaScript function that takes in a number to be rounded as the first argument and the rounding off factor as the second argument.The function should return the result after rounding off the number.ExampleThe code for this will be −const num = 76; const ...

Read More

Find specific key value in array of objects using JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 3K+ Views

Suppose we have a JSON object like this −const obj = {    "LAPTOP": [{       "productId": "123"    }],    "DESKTOP": [{       "productId": "456"    }],    "MOUSE": [{       "productId": "789"    }, {       "productId": "012"    }],    "KEY-BOARD": [{       "productId": "345"    }] };We are required to write a JavaScript function that takes in one such object as the first argument, and a key value pair as the second argument.The key value pair is basically nothing but an object like this −const pair ...

Read More

Generating Random Prime Number in JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 668 Views

We are required to write a JavaScript function that takes in two numbers specifying a range. Our function should return a random prime number that falls in that rangeExampleThe code for this will be −const range = [100, 1000]; const getPrimes = (min, max) => {    const result = Array(max + 1)    .fill(0)    .map((_, i) => i);    for (let i = 2; i {    return Math.floor(Math.random() * (max − min + 1) + min); }; const getRandomPrime = ([min, max]) => {    const primes = getPrimes(min, max);    return primes[getRandomNum(0, primes.length − 1)]; }; console.log(getRandomPrime(range));OutputAnd the output in the console will be −311Output is likely to differ in each run.

Read More

Iterating through an array, adding occurrences of a true in JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 171 Views

Suppose we have an array of true/false represented by 't'/'f' which we retrieved from some database like this −const arr = ['f', 't', 'f', 't', 't', 't', 'f', 'f', 't', 't', 't', 't', 't', 't', 'f', 't'];We are required to write a JavaScript function that takes in one such array. Our function should count the consecutive appearances of those 't' that are sandwiched between two 'f's and return an array of that count.Therefore, for the above array, the output should look like −const output = [1, 3, 6, 1];ExampleThe code for this will be −const arr = ['f', 't', 'f', ...

Read More
Showing 5031–5040 of 8,392 articles
« Prev 1 502 503 504 505 506 840 Next »
Advertisements