Found 9150 Articles for Object Oriented Programming

How to calculate the average in JavaScript of the given properties in the array of objects

AmitDiwan
Updated on 24-Aug-2020 09:42:21

1K+ Views

We have an array of objects. Each object contains a few properties and one of these properties is age −const people = [    {       name: 'Anna',       age: 22    }, {       name: 'Tom',       age: 34    }, {       name: 'John',       age: 12    }, {       name: 'Kallis',       age: 22    }, {       name: 'Josh',       age: 19    } ]We have to write a function that takes in such an ... Read More

Sum of even numbers from n to m regardless if nm JavaScript

AmitDiwan
Updated on 24-Aug-2020 09:38:30

274 Views

We are required to write a function that takes two numbers as arguments m and n, and it returns the sum of all even numbers that falls between m and n (both inclusive)For example −If m = 10 and n = -4The output should be 10+8+6+4+2+0+(-2)+(-4) = 24ApproachWe will first calculate the sum of all even numbers up to n and the sum of all even numbers up to m.Then we will check for the bigger of the two m and n. Subtract the sum of smaller from the sum of bigger which will eventually give us the sum between ... Read More

Sort array of points by ascending distance from a given point JavaScript

AmitDiwan
Updated on 24-Aug-2020 09:36:40

2K+ Views

Let’s say, we have an array of objects with each object having exactly two properties, x and y that represent the coordinates of a point. We have to write a function that takes in this array and an object with x and y coordinates of a point and we have to sort the points (objects) in the array according to the distance from the given point (nearest to farthest).The Distance FormulaIt is a mathematical formula that states that the shortest distance between two points (x1, y1) and (x2, y2) in a two-dimensional plane is given by −$S=\sqrt{((x2-x1)^2+(y2-y1)^2)}$We will be using ... Read More

JavaScript Match between 2 arrays

AmitDiwan
Updated on 24-Aug-2020 09:30:49

336 Views

Let’s say, we have two arrays, one of String literals and another of objects.const data = [{    name: 'Kamlesh Kapasi',    uid: 123 }, {       name: 'Mahesh Babu',       uid: 129 }, {       name: 'Akshay Kapoor',       uid: 223 }, {       name: 'Vikas Gupta',       uid: 423 }, {       name: 'Mohit Dalal',       uid: 133 }, {       name: 'Rajkumar Hirani',       uid: 233 }, {       name: 'Joy',       uid: ... Read More

How to move multiple elements to the beginning of the array in JavaScript?

AmitDiwan
Updated on 24-Aug-2020 09:26:52

744 Views

We have to write a function that takes an array and any number of strings as arguments. The task is to check if the strings occur within the array. If it does, we have to move that particular to the front of the array.Therefore, let’s write the code for this function −Exampleconst arr = ['The', 'weather', 'today', 'is', 'a', 'bit', 'windy.']; const pushFront = (arr, ...strings) => {    strings.forEach(el => {       const index = arr.indexOf(el);       if(index !== -1){          arr.unshift(arr.splice(index, 1)[0]);       };    }); }; pushFront(arr, 'today', ... Read More

Insert value in the middle of every value inside array JavaScript

AmitDiwan
Updated on 24-Aug-2020 09:24:57

357 Views

We have an array of numbers like this −const numbers = [1, 6, 7, 8, 3, 98];We have to convert this array of numbers into an array of objects with each object having a key as “value” and its value as a specific value of the array element. Besides this we have to insert object between two pre-existing elements with key as “operation” and using alternatively one of +, - * , / as its value.Therefore, for the numbers array, the output would look something like this −[    { "value": 1 }, { "operation": "+" }, { "value": 6 ... Read More

Check if values of two arrays are the same/equal in JavaScript

AmitDiwan
Updated on 24-Aug-2020 09:10:59

626 Views

We have two arrays of numbers, let’s say −[2, 4, 6, 7, 1] [4, 1, 7, 6, 2]Assume, we have to write a function that returns a boolean based on the fact whether or not they contain the same elements irrespective of their order.For example −[2, 4, 6, 7, 1] and [4, 1, 7, 6, 2] should yield true because they have the same elements but ordered differently.Now, let’s write the code for this function −Exampleconst first = [2, 4, 6, 7, 1]; const second = [4, 1, 7, 6, 2]; const areEqual = (first, second) => {    if(first.length ... Read More

Strictly increasing sequence JavaScript

AmitDiwan
Updated on 24-Aug-2020 09:09:08

1K+ Views

Given a sequence of integers as an array, we have to determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.For example −For sequence = [1, 3, 2, 1], the output should be function(sequence) = false. There is no one element in this array that can be removed in order to get a strictly increasing sequence.For sequence = [1, 3, 2], the output should be function(sequence) = true. You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to ... Read More

Filter away object in array with null values JavaScript

AmitDiwan
Updated on 24-Aug-2020 09:07:24

2K+ Views

Let’s say, we have an array of objects about some employees of a company. But the array contains some bad data i.e., key pointing to empty strings or false values. Our job is to write a function that takes in the array and away the objects that have null or undefined or empty string value for the name key and return the new object.The array of objects are like this −let data = [{    "name": "Ramesh Dhiman",    "age": 67,    "experience": 45,    "description": "" }, {       "name": "",       "age": 31,   ... Read More

Create a Calculator function in JavaScript

AmitDiwan
Updated on 24-Aug-2020 08:59:06

816 Views

We have to write a function, say calculator() that takes in one of the four characters (+, - , *, / ) as the first argument and any number of Number literals after that. Our job is to perform the operation specified as the first argument over those numbers and return the result.If the operation is multiplication or addition, we are required to perform the same operation with every element. But if the operation is subtraction or division, we have to consider the first element as neutral and subtract all other elements from it or divide it by all other ... Read More

Advertisements