Join JavaScript Array of Strings

AmitDiwan
Updated on 01-Oct-2020 11:03:47

148 Views

We are required to write a JavaScript function that takes in an array of strings. The function should join all the strings of the array, replace all the whitespaces with dash "-", and return the string thus formed.For example: If the array is −const arr = ["QA testing promotion ", " Twitter  ", "Facebook ", "Test"];Then the output should be −const output = "QA-testing-promotion-Twitter-Facebook-Test";ExampleFollowing is the code −const arr = ["QA testing promotion ", " Twitter ", "Facebook ", "Test"]; const joinArr = arr => {    const arrStr = arr.join('');    let res = '';    for(let i = ... Read More

Remove Negatives from the Array in JavaScript

AmitDiwan
Updated on 01-Oct-2020 11:02:20

300 Views

Given an array X of multiple values (e.g. [-3, 5, 1, 3, 2, 10]), We are required to write a function that removes any negative values in the array.Once the function finishes its execution the array should be composed of just positive numbers. We are required to do this without creating a temporary array and only using pop method to remove any values in the array.ExampleFollowing is the code −// strip all negatives off the end while (x.length && x[x.length - 1] < 0) {    x.pop(); } for (var i = x.length - 1; i >= 0; i--) { ... Read More

Finding Number That Appears for Odd Times in JavaScript

AmitDiwan
Updated on 01-Oct-2020 11:00:01

169 Views

Given an array of integers, we are required to write a function that takes this array and finds the one element that appears an odd number of times. There will always be only one integer that appears an odd number of times.We will approach this problem by sorting the array. Once sorted, we can iterate over the array to pick the element that appears for odd number of times.ExampleFollowing is the code −const arr = [20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]; const findOdd = arr => {    let ... Read More

Find the Biggest Number in an Array with Undefined Elements in JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:57:35

233 Views

We are required to write a JavaScript function that takes in an array that contains some numbers, some strings and some falsy values.Our function should return the biggest Number from the array.For example −If the input array is the following with some undefined values −const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii'];Then the output should be 65ExampleFollowing is the code −const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; const pickBiggest = arr => {    let max = -Infinity;    for(let i = 0; i < arr.length; i++){     ... Read More

Select the Middle of an Array in JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:55:02

603 Views

We are required to write a JavaScript function that takes in an array of Numbers.The function should return the middlemost element of the array.For example −If the array is −const arr = [1, 2, 3, 4, 5, 6, 7];Then the output should be 4ExampleFollowing is the code −const arr = [1, 2, 3, 4, 5, 6, 7]; const middle = function(){    const half = this.length >> 1;    const offset = 1 - this.length % 2;    return this.slice(half - offset, half + 1); }; Array.prototype.middle = middle; console.log(arr.middle()); console.log([1, 2, 3, 4, 5, 6].middle());OutputThis will produce the following output on console −[ 4 ] [ 3, 4 ]

Convert Array into Array of Subarrays in JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:52:32

987 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]]ExampleFollowing is the code −const arr = [1, 2, 3, 4, 5, 6, 7]; const chunk ... Read More

Merge Two Objects in JavaScript Ignoring Undefined Values

AmitDiwan
Updated on 01-Oct-2020 10:51:13

2K+ Views

Suppose, we have two objects, say A and B like these −const A = { activity: 'purchased', count: undefined, time: '09:05:33' }; const B = { activity: 'purchased', count: '51', time: undefined }; We are required to write a JavaScript function that merges these two objects, keeping in mind if any key has a truthy value then it should not be overwritten by a key having falsy value.If we do this simply by using the spread operator, it will not keep track of truth or falsy values.Therefore, we have to do this using an iterative approach.ExampleFollowing is the code −const A ... Read More

Sort an Array to Have Specific Items First in JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:49:03

2K+ 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

Sort Array of Objects by String Property Value in JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:47:18

315 Views

Suppose, we have an array of Objects like this −const arr = [    { first_name: 'Lazslo', last_name: 'Jamf'     },    { first_name: 'Pig',    last_name: 'Bodine'   },    { first_name: 'Pirate', last_name: 'Prentice' } ];We are required to write a JavaScript function that takes in one such array and sort this array according to the alphabetical value of the last_name key.ExampleFollowing is the code −const arr = [    { first_name: 'Lazslo', last_name: 'Jamf' },    { first_name: 'Pig', last_name: 'Bodine' },    { first_name: 'Pirate', last_name: 'Prentice' } ]; const sortByLastName = arr => { ... Read More

Grouping Array Nested Value While Comparing 2 Objects in JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:45:30

407 Views

Suppose, we have the following JSON object −const input = {    "before": {      "device": [        {          "id": "1234",          "price": "10",          "features": [            {              "name": "samsung",              "price": "10"            },            {              "name": "Apple",              "price": "20"            }         ... Read More

Advertisements