Articles on Trending Technologies

Technical articles with clear explanations and examples

Find the shortest string in an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 773 Views

We are required to write a JavaScript function that takes in an array of strings and returns the index of string that is shortest in length.We will simply use a for loop and persist the index of string which is shortest in length.ExampleFollowing is the code −const arr = ['this', 'can', 'be', 'some', 'random', 'sentence']; const findSmallest = arr => {    const creds = arr.reduce((acc, val, index) => {       let { ind, len } = acc;       if(val.length < len){          len = val.length;          ind = index; ...

Read More

Sum all similar elements in one array - JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 454 Views

We are required to write a JavaScript function that takes in an array of Numbers and sums all the identical numbers together at one indexFor example −If the input array is −const arr = [20, 10, 15, 20, 15, 10];Then the output should be −const output = [40, 20, 30];ExampleFollowing is the code −const arr = [20, 10, 15, 20, 15, 10]; const addSimilar = arr => {    for(let i = 0; i < arr.length; i++){       while(i !== arr.lastIndexOf(arr[i])){          const ind = arr.lastIndexOf(arr[i]);          arr[i] += arr.splice(ind, 1)[0];       };    }; }; addSimilar(arr); console.log(arr);OutputThis will produce the following output in console −[ 40, 20, 30 ]

Read More

Alternatively merging two arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 832 Views

We are required to write a JavaScript function that takes in two arrays and merges the arrays taking elements alternatively from the arrays.For example −If the two arrays are −const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3];Then the output should be −const output = [4, 2, 3, 1, 2, 6, 5, 8, 6, 9, 8, 4, 9, 3];ExampleFollowing is the code −const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3]; const mergeAlernatively = (arr1, arr2) => {    const res = [];    for(let i = 0; i < arr1.length + arr2.length; i++){       if(i % 2 === 0){          res.push(arr1[i/2]);       }else{          res.push(arr2[(i-1)/2]);       };    };    return res; }; console.log(mergeAlernatively(arr1, arr2));OutputThis will produce the following output in console −[    4, 2, 3, 1, 2, 6,    5, 8, 6, 9, 8, 4,    9, 3 ]

Read More

Figuring out the highest value through a for in loop - JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 160 Views

Suppose, we have a comma separator string that contains some fruit names like this −const str = 'Banana, Banana, Pear, Orange, Apple, Melon, Grape, Apple, Banana, Grape, Melon, Grape, Melon, Apple, Grape, Banana, Orange, Melon, Orange, Banana, Banana, Orange, Pear, Grape, Orange, Orange, Apple, Apple, Banana';We are required to write a JavaScript function that takes in one such string and uses the for in loop to figure out the fruit name that appears for the greatest number of times in the string.The function should return the fruit string that appears for most number of times.ExampleFollowing is the code −const str ...

Read More

Creating an array using a string which contains the key and the value of the properties - JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 116 Views

Suppose, we have a special kind of string like this −const str ="Integer, 1 Float, 2.0Boolean, True Integer, 6Float, 3.66 Boolean, False";We are required to write a JavaScript function that converts the above string into the following array, using the String.prototype.split() method −const arr = [    {        "Integer":1,        "Float":2.0    },    {        "Boolean":true,        "Integer":6    },    {        "Float":3.66,        "Boolean":false    } ];We have to use the following rules for conversion −--- marks the end of an object --- ...

Read More

Finding reversed index of elements in arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 1K+ Views

We are required to write a JavaScript function that takes in an array of String/Number literals as the first argument and a String/Number as the second argument.If the variable taken as the second argument is not present in the array, we should return -1.Else if the number is present in the array, then we have to return the index of position the number would have occupied if the array were reversed. We have to do so without actually reversing the array.Then at last we have to attach this function to the Array.prototype object.For example −[45, 74, 34, 32, 23, 65].reversedIndexOf(23); ...

Read More

Sum of all prime numbers in an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 1K+ Views

We are required to write a JavaScript function that takes in an array of numbers.The function should return the sum of all the prime numbers present in the array.Let’s say the following is our array −const arr = [43, 6, 6, 5, 54, 81, 71, 56, 8, 877, 4, 4];The function should sum the prime numbers i.e.43 + 5 + 71 + 877 = 996ExampleFollowing is the code −const arr = [43, 6, 6, 5, 54, 81, 71, 56, 8, 877, 4, 4]; const isPrime = n => {    if (n===1){       return false;    }else if(n ...

Read More

Check if a string is sorted in JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 567 Views

We are required to write a JavaScript function that takes in a string and checks whether it is sorted or not.For example −isSorted('adefgjmxz')  // true isSorted('zxmfdba')     // true isSorted('dsfdsfva')     // falseExampleFollowing is the code −const str = 'abdfhlmxz'; const findDiff = (a, b) => a.charCodeAt(0) - b.charCodeAt(0); const isStringSorted = (str = '') => {    if(str.length < 2){       return true;    };    let res = ''    for(let i = 0; i < str.length-1; i++){       if(findDiff(str[i+1], str[i]) > 0){          res += 'u';     ...

Read More

Sorting an array object by property having falsy value - JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 251 Views

Suppose, we have an array of objects like this −const array = [    {key: 'a', value: false},    {key: 'a', value: 100},    {key: 'a', value: null},    {key: 'a', value: 23} ];We are required to write a JavaScript function that takes in one such array and places all the objects that have falsy values for the "value" property to the bottom and sorts all other objects in decreasing order by the "value" property.ExampleFollowing is the code −const arr = [    {key: 'a', value: false},    {key: 'a', value: 100},    {key: 'a', value: null},    {key: 'a', ...

Read More

Counting occurrences of vowel, consonants - JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 577 Views

We are required to write a JavaScript function that takes in a string which contains English alphabet, for example −const str = 'This is a sample string, will be used to collect some data';The function should return an object containing the count of vowels and consonants in the string i.e. the output should be −{ vowels: 17, consonants: 29 }ExampleFollowing is the code −const str = 'This is a sample string, will be used to collect some data'; const countAlpha = str => {    return str.split('').reduce((acc, val) => {       const legend = 'aeiou';       ...

Read More
Showing 37891–37900 of 61,248 articles
Advertisements