Found 9150 Articles for Object Oriented Programming

Chunking arrays in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:05:57

158 Views

We are required to write a JavaScript function that takes in an array of literals and returns a new array that have elements of the original array chunked into subarrays of length exactly 2. Now if the length of original array is not exactly divisible by 2, then the last subarray should have only one element.For example: If the input array is −const arr = [1, 2, 3, 4, 5, 6, 7];Then the output should be −const output = [[1, 2], [3, 4], [5, 6], [7]]Therefore, let’s write the code for this function −ExampleThe code for this will be −const ... Read More

Remove leading zeros in array in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:04:32

444 Views

The requirements for this question are simple. We are required to write a JavaScript function that takes in an array of Numbers.If the array contains leading zero, the function should remove the leading zeros in place, otherwise the function should do nothing.For example: If the input array is −const arr = [0, 0, 0, 14, 0, 63, 0];Then the output should be −const output = [14, 0, 63, 0];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [0, 0, 0, 14, 0, 63, 0]; const removeLeadingZero = arr => {    while (arr.indexOf(0) === 0) {       arr.shift();    }; }; removeLeadingZero(arr); console.log(arr);OutputThe output in the console will be −[ 14, 0, 63, 0 ]

Push specific elements to last in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:02:58

134 Views

Suppose we have an array of objects like this −const arr = [    {flag: true, other: 1},    {flag: true, other: 2},    {flag: false, other: 3},    {flag: true, other: 4},    {flag: true, other: 5},    {flag: true, other: 6},    {flag: false, other: 7} ];We are required to write a JavaScript function that takes in one such array and sorts it based on the following conditions −If arr.flag === false, the matching element gets placed first in the array, but only after the previous matching elements.The elements that do not match, remain in the same order ... Read More

Finding the balance of brackets in JavaScript

AmitDiwan
Updated on 24-Oct-2020 11:58:53

344 Views

Given a string that consists of only two types of characters: "(" and ")". We are required to write a function that takes in one such string and balances the parentheses by inserting either a "(" or a ")" as many times as necessary. The function should then return the minimum number of insertions made in the string to balance it.For example: If the string is −const str = '()))';Then the output should be 2, because by prepending '((', we can balance the string.ExampleThe code for this will be −const str = '()))'; const balanceParanthesis = str => {   ... Read More

Is element repeated more than n times in JavaScript

AmitDiwan
Updated on 24-Oct-2020 11:56:59

169 Views

We are required to write a JavaScript function that takes in two arguments −An Array, say arr, of literals that may contain some repeating elements.A number, say limit.The function should validate that no element of the array is repeated more than limit number of times. If any element is repeated more than the limit the function should return false, true otherwise.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [4, 6, 7, 4, 2, 5, 7, 7, 4, 4, 3]; const validateElements = (arr, n) => {    const counts = arr.reduce((acc, ... Read More

Writing table of number in array in JavaScript

AmitDiwan
Updated on 24-Oct-2020 11:55:22

137 Views

We are required to write a JavaScript function that takes in two numbers, say m and n, and it returns an array of first n multiples of m.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num1 = 4; const num2 = 6; const multiples = (num1, num2) => {    const res = [];    for(let i = num1; i

Counting smaller and greater in JavaScript

AmitDiwan
Updated on 24-Oct-2020 11:54:09

350 Views

Suppose, we have an array of literals like this −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9];We are required to write a JavaScript function that takes in this array and a number, say n, and returns an object representing the count of elements greater than and smaller than n.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9]; const smallerLargerNumbers = (arr, num) => {    return arr.reduce((acc, val) => {       let { greater, smaller ... Read More

Counting unique elements in an array in JavaScript

AmitDiwan
Updated on 24-Oct-2020 11:52:26

2K+ Views

We are required to write a JavaScript function that counts all unique items in an array. The function should return an object representing the count of each unique element of the array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = ["hi", "hello", "hi"]; const countUnique = arr => {    const counts = {};    for (var i = 0; i < arr.length; i++) {       counts[arr[i]] = 1 + (counts[arr[i]] || 0);    };    return counts; }; console.log(countUnique(arr));OutputThe output in the console will be −{ hi: 2, hello: 1 }

Index of closest element in JavaScript

AmitDiwan
Updated on 24-Oct-2020 11:51:40

197 Views

Suppose we have an array like this −const arr = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362];We are required to write a JavaScript function that takes in one such array and a number, say n.The function should return the index of item from the array which is closest to the number n.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362]; const closestIndex = (num, arr) => {    let curr = arr[0], diff = Math.abs(num - curr);    let ... Read More

Repeating only even numbers inside an array in JavaScript

AmitDiwan
Updated on 22-Oct-2020 13:27:19

189 Views

We are required to write a JavaScript function that should repeat the even number inside the same array.For example, given the following array −const arr = [1, 2, 5, 6, 8];OutputWe should get the output −const output = [1, 2, 2, 5, 6, 6, 8, 8];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [1, 2, 5, 6, 8]; const repeatEvenNumbers = arr => {    let end = arr.length -1;    for(let i = end; i > 0; i--){       if(arr[i] % 2 === 0){          arr.splice(i, 0, arr[i]);       };    };    return arr; }; console.log(repeatEvenNumbers(arr));OutputThe output in the console will be −[    1, 2, 2, 5,    6, 6, 8, 8 ]

Advertisements