Found 9150 Articles for Object Oriented Programming

JavaScript to push value in empty index in array

Disha Verma
Updated on 25-Feb-2025 14:57:30

1K+ Views

To push value in an empty index in an array, we must write an array function pushAtEmpty() that takes in an element and pushes it at the first empty index it finds in the array it is used in the context of. If there are no empty spaces, the element should be pushed to the end of the array. The arrays are data structures in JavaScript that are used to store values of the same type in a linear form. Arrays are basic data structures that every developer works on. While working with arrays, they commonly work on pushing elements ... Read More

Check for Power of two in JavaScript

AmitDiwan
Updated on 26-Aug-2020 09:42:28

2K+ Views

We are required to write a function, say isPowerOfTwo() that takes in a positive number and returns a boolean based on the fact whether or not the number is some power of 2.For example −console.log(isPowerOfTwo(3)); //false console.log(isPowerOfTwo(32)); //true console.log(isPowerOfTwo(2048)); //true console.log(isPowerOfTwo(256)); //true console.log(isPowerOfTwo(22)); //falseLet’s write the code for this function, it will be a very straightforward recursive function that keeps recurring until the number stays divisible by 2, if in this process the number gets reduced all the way down to 1, it is a power of 2 otherwise it isn’t. Here is the code −Exampleconst isPowerOfTwo = num => ... Read More

How do I write a function that takes an array of values and returns an object JavaScript?

AmitDiwan
Updated on 26-Aug-2020 09:41:04

562 Views

Let’s say, we are required to write a function classifyArray() that takes in an array which contains mixed data types and returns a Map() with the elements grouped by their data types.For example −// if the input array is: const arr = ['class', 2, [7, 8, 9], {"name": "Michael"}, Symbol('foo'), true, false, 'name', 6]; // then the output Map should be: Map(5) {    'string' => [ 'class', 'name' ],    'number' => [ 2, 6 ],    'object' => [ [ 7, 8, 9 ], { name: 'Michael' } ],    'symbol' => [ Symbol(foo) ],    'boolean' => ... Read More

Recursively flat an object JavaScript

AmitDiwan
Updated on 26-Aug-2020 09:38:41

390 Views

We are required to write a function that does the following transformation −If the input object is −const input = {    a: 0,    b: {x: {y: 1, z: 2}},    c: 3 };Then the output of the function should be −const output = {    a: 0,    'b.x.y': 1,    'b.x.z': 2,    c: 3 }We basically have to write a function that flattens a nested object. Will do this via recursion and the code for doing this will be −Exampleconst obj = {    a: 1,    b: {x: {y: 1, z: 2}},    c: 3 ... Read More

Parse array to equal intervals in JavaScript

AmitDiwan
Updated on 26-Aug-2020 09:36:25

208 Views

Let’s say, we are required to write a function, say parseEqualInterval() that takes in an array of Numbers of strictly two elements as the first argument and a number n as the second argument and it inserts n-1 equidistant entries between the actual two elements of the original array so that it gets divided into n equal intervals.For example −// if the input array is const arr = [12, 48]; // and the interval is 4 //then the output array should be: const output = [12, 21, 30, 39, 48];This way the array got divided into 4 equal intervals. So, ... Read More

How to check if every property on object is the same recursively in JavaScript?

AmitDiwan
Updated on 26-Aug-2020 09:34:21

427 Views

Let’s say, we are required to write a function, say isSame() that accepts a nested object and returns a boolean depending on the fact whether or not all the keys have the same values. When saying all the keys we mean all the last keys like if a key has a nested object as its value, we are required to traverse to the end of the nested object and check for that value.For example − If the object is −const obj = {    a: 1,    b: 1,    c: {       aa: 1    } };Then ... Read More

Rearrange an array in maximum minimum form by JavaScript

AmitDiwan
Updated on 26-Aug-2020 09:31:44

268 Views

We are required to write a function, say minMax() that takes in an array of Numbers and rearranges the elements such that the greatest element appears first followed by the smallest elements then the second greatest element followed by second smallest element and so on.For example −// if the input array is: const input = [1, 2, 3, 4, 5, 6, 7] // then the output should be: const output = [7, 1, 6, 2, 5, 3, 4]So, let’s write the complete code for this function −Exampleconst input = [1, 2, 3, 4, 5, 6, 7]; const minMax = arr ... Read More

How to count the number of elements in an array below/above a given number (JavaScript)

AmitDiwan
Updated on 26-Aug-2020 09:30:08

209 Views

Consider we have an array of Numbers that looks like this −const array = [3.1, 1, 2.2, 5.1, 6, 7.3, 2.1, 9];We are required to write a function that counts how many of the elements are in the array below / above a given number.For example, if the number is 5.25, the answer should be the following 5 elements, (3.1, 1, 2.2, 5.1, 2.1)and 3 elements above it −(6, 7.3, 9)Note − If any element is equal to the provided number, it should be counted as above the number.So, let’s write the code for this function −Exampleconst array = [3.1, ... Read More

Group values in array by two properties JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:56:05

1K+ Views

We have an array of objects like this −const arr = [    { value: 12, gap: 1 },    { value: 13, gap: 1 },    { value: 14, gap: 1 },    { value: 15, gap: 1 },    { value: 19, gap: 2 },    { value: 21, gap: 1 },    { value: 22, gap: 1 },    { value: 23, gap: 1 },    { value: 27, gap: 1 },    { value: 31, gap: 4 },    { value: 35, gap: 4 },    { value: 39, gap: 4 },    { value: 43, ... Read More

Form Object from string in JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:53:01

263 Views

We are required to write a function that takes in a string as the first and the only argument and constructs an object with its keys based on the unique characters of the string and value of each key being defaulted to 0.For example −// if the input string is: const str = 'hello world!'; // then the output should be: const obj = {"h": 0, "e": 0, "l": 0, "o": 0, " ": 0, "w": 0, "r": 0, "d": 0, "!": 0};So, let’s write the code for this function −Exampleconst str = 'hello world!'; const stringToObject = str => ... Read More

Advertisements