Found 9326 Articles for Object Oriented Programming

Remove duplicates from array with URL values in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:43:10

340 Views

Suppose, we have an array of objects like this −const arr = [    {       url: 'www.example.com/hello',       id: "22"    },    {       url: 'www.example.com/hello',       id: "22"    },    {       url: 'www.example.com/hello-how-are-you',       id: "23"    },    {       url: 'www.example.com/i-like-cats',       id: "24"    },    {       url: 'www.example.com/i-like-pie',       id: "25"    } ];We are required to write a JavaScript function that takes in one such array of objects. The ... Read More

Compare keys & values in a JSON object when one object has extra keys in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:41:17

2K+ Views

Suppose, we have two JSON objects like these −const obj1 = {a: "apple", b: "banana", c: "carrot"}; const obj2 = {a: "apple", e: "egg", b: "banana", c: "carrot", d: "dog"};We are required to write a JavaScript function that takes in two such objects. We want to be able to have a Boolean check comparing the two objects without having to remove data from either one.For example, if I were to use the data above, the Boolean check should return true because the values of the keys that are in both objects match.However, let’s say obj1 stays the same but obj2 ... Read More

Palindrome numbers in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:40:02

556 Views

We are required to write a JavaScript function that takes in a number and determines whether or not it is a palindrome number.Palindrome numbers − A palindrome number is that number which reads the same from both left and right sides.For example −343 is a palindrome number6789876 is a palindrome number456764 is not a palindrome numberExampleThe code for this will be −const num1 = 343; const num2 = 6789876; const num3 = 456764; const isPalindrome = num => {    let length = Math.floor(Math.log(num) / Math.log(10) +1);    while(length > 0) {       let last = Math.abs(num − ... Read More

Check for a self-dividing number in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:38:31

168 Views

We are required to write a JavaScript function that takes in a number and determines whether or not it is a self−dividing number.A self−dividing number is a number that is divisible by every digit it contains.It should output “This number is self−dividing” if it is otherwise, it should output “This number is NOT self−dividing”.For example, 128 is a self−dividing number because 1, 2, and 8 are all divisors of 128.Another example, 102 is not a self−diving number because it contains a digit 0.As a 3rd example, 26 is not a self−dividing number, because it’s not divisible by 6.ExampleThe code for ... Read More

Complete Equation by Filling Missing Operator in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:37:11

272 Views

We are required to write a JavaScript function that takes in a bunch of numbers and returns the correct sequence of operations to satisfy the equation. The operator that can be used are (+, −, *, /, ^, %).For example −Input : 5 3 8 Output : 5+3=8 Input : 9 27 3 Output : 9=27/3 Input : 5 2 25 , 1 5 2 Output : 5^2=25 , 1=5%2For each input, there is at least one possible sequence, we are required to ... Read More

How to get the most common values in array: JavaScript ?

AmitDiwan
Updated on 21-Nov-2020 09:34:54

361 Views

We are required to write a JavaScript function that takes in an array of literals that have repeating values. Our function should return an array of the most common element(s) in the array (if two or more elements appear for the same number of most times then the array should contain all those elements).ExampleThe code for this will be −const arr1 = ["a", "c", "a", "b", "d", "e", "f"]; const arr2 = ["a", "c", "a", "c", "d", "e", "f"]; const getMostCommon = arr => {    const count = {};    let res = [];    arr.forEach(el => {   ... Read More

Get intersection between two ranges in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:35:56

619 Views

Suppose, we have two arrays of numbers that represents two ranges like these −const arr1 = [2, 5]; const arr2 = [4, 7];We are required to write a JavaScript function that takes in two such arrays.The function should then create a new array of range, that is the intersection of both the input ranges and return that range.Therefore, the output for the above input should look like this −const output = [4, 5];ExampleThe code for this will be −const arr1 = [2, 5]; const arr2 = [4, 7]; const findRangeIntersection = (arr1 = [], arr2 = []) => {   ... Read More

How to create a third object from two objects using the key values in JavaScript?

AmitDiwan
Updated on 21-Nov-2020 09:33:39

153 Views

Suppose, we have two objects like these −const obj1 = {    positive: ['happy', 'excited', 'joyful'],    negative: ['depressed', 'sad', 'unhappy'] }; const obj2 = {    happy: 6,    excited: 1,    unhappy: 3 };We are required to write a JavaScript function that takes in two such objects. The function should then use both these objects to calculate the positive and negative scores and return an object like this −const output = {positive: 7, negative: 3};ExampleThe code for this will be −const obj1 = {    positive: ['happy', 'excited', 'joyful'],    negative: ['depressed', 'sad', 'unhappy'] }; const obj2 = ... Read More

Group all the objects together having the same value for the '_id' key in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:33:44

241 Views

Suppose we have an array of objects like this −const arr = [    {_id : "1", S : "2"},    {_id : "1", M : "4"},    {_id : "2", M : "1"},    {_id : "" , M : "1"},    {_id : "3", S : "3"} ];We are required to write a JavaScript function that takes in one such array and groups all the objects together that have the same value for the '_id' key.Therefore, the final output should look something like this −const output = [    {_id : "1", M : "4", S : "2", ... Read More

Find the minimum and maximum values that can be calculated by summing exactly four of the five integers in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:32:20

2K+ Views

Given an array of five positive integers, we are required to find the minimum and maximum values that can be calculated by summing exactly four of the five integers.Then print the respective minimum and maximum values as a single line of two spaceseparated long integers.The array is not sorted all the times.For example −const arr = [1, 3, 5, 7, 9]The minimum sum is −1 + 3 + 5 + 7 = 16and the maximum sum is −3 + 5 + 7 = 24The return value of the function should be −[16, 24];ExampleThe code for this will be −const arr ... Read More

Advertisements