Found 6686 Articles for Javascript

Return a map representing the frequency of each data type in an array in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:17:31

74 Views

We are required to write a JavaScript function that takes in an array that contains elements of different data types and the function should return a map representing the frequency of each data type.Let’s say the following is our array −const arr = [23, 'df', undefined, null, 12, {    name: 'Rajesh' }, [2, 4, 7], 'dfd', null, Symbol('*'), 8];ExampleFollowing is the code to return a map representing the frequency of each datatype −const arr = [23, 'df', undefined, null, 12, {    name: 'Rajesh' }, [2, 4, 7], 'dfd', null, Symbol('*'), 8]; const countDataTypes = arr => {   ... Read More

Finding the continuity of two arrays in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:15:50

101 Views

We are required to write a JavaScript function that takes in two arrays of numbers. And the function should return true if the two arrays upon combining and shuffling can form a consecutive sequence, false otherwise.For example: If the arrays are −const arr1 = [4, 6, 2, 9, 3]; const arr2 = [1, 5, 8, 7];Then the output should be true.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = [4, 6, 2, 9, 3]; const arr2 = [1, 5, 8, 7]; const canFormSequence = (arr1, arr2) => {    const combined = ... Read More

Function to find the length of the second smallest word in a string in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:13:53

108 Views

We are required to write a JavaScript function that takes in a string sentence as first and the only argument. And the function should return the length of the second smallest word from the string.For example: If the string is −const str = 'This is a sample string';Then the output should be 2.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'This is a sample string'; const secondSmallest = str => {    const strArr = str.split(' ');    if(strArr.length < 2){       return false;    }    for(let i ... Read More

Function that only replaces character from string after specified appearances in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:12:19

62 Views

We are required to write a JavaScript function that takes in a string as the first argument, a number, say n, as the second argument and a character, say c, as the third argument. The function should replace the nth appearance of any character with the character provided as the third argument and return the new string.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'This is a sample string'; const num = 2; const char = '*'; const replaceNthAppearance = (str, num, char) => {    const creds = str.split('').reduce((acc, val, ... Read More

Return a subarray that contains all the element from the original array that are larger than all the elements on their right in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:10:37

77 Views

We are required to write a JavaScript function that takes in an array of numbers and returns a subarray that contains all the element from the original array that are larger than all the elements on their right.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [12, 45, 6, 4, 23, 23, 21, 1]; const largerThanRight = (arr = []) => {    const creds = arr.reduceRight((acc, val) => {       let { largest, res } = acc;       if(val > largest){          res.push(val);          largest = val;       };       return { largest, res };    }, {       largest: -Infinity,       res: []    });    return creds.res; }; console.log(largerThanRight(arr));OutputThe output in the console will be −[ 1, 21, 23, 45 ]

Finding the validity of a hex code in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:09:04

118 Views

A string can be considered as a valid hex code if it contains no characters other than the 0-9 and a-f alphabets.For example:'3423ad' is a valid hex code '4234es' is an invalid hex codeWe are required to write a JavaScript function that takes in a string and checks whether its a valid hex code or not.ExampleThe code for this will be −const str1 = '4234es'; const str2 = '3423ad'; const isHexValid = str => {    const legend = '0123456789abcdef';    for(let i = 0; i < str.length; i++){       if(legend.includes(str[i])){          continue;     ... Read More

How many times can we sum number digits in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:48:08

108 Views

We are required to write a JavaScript function that takes in an positive integer and returns its additive persistence.The additive persistence of an integer, say n, is the number of times we have to replace the number with the sum of its digits until the number becomes a single digit integer.For example: If the number is −1679583Then, 1 + 6 + 7 + 9 + 5 + 8 + 3 = 39 // 1 Pass 3 + 9 = 12 // 2 Pass 1 + 2 = 3 // 3 PassTherefore, the output should be 3.ExampleThe code for this will ... Read More

Check if the elements of the array can be rearranged to form a sequence of numbers or not in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:46:11

107 Views

We are required to write a JavaScript function that takes in an array of numbers and checks if the elements of the array can be rearranged to form a sequence of numbers or not.For example: If the array is −const arr = [3, 1, 4, 2, 5];Then the output should be true.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [3, 1, 4, 2, 5]; const canBeConsecutive = (arr = []) => {    if(!arr.length){       return false;    };    const copy = arr.slice();    copy.sort((a, b) => a ... Read More

Finding special kind of sentences (smooth) in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:44:04

116 Views

We are required to write a JavaScript function that checks whether a sentence is smooth or not.A sentence is smooth when the first letter of each word in the sentence is the same as the last letter of its preceding word.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'this stringt tries sto obe esmooth'; const str2 = 'this string is not smooth'; const isSmooth = str => {    const strArr = str.split(' ');    for(let i = 0; i < strArr.length; i++){       if(!strArr[i+1] || strArr[i][strArr[i].length -1] ===strArr[i+1][0]){ ... Read More

Finding the inclination of arrays in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:40:33

77 Views

We are required to write a JavaScript function that takes in an array of numbers and returns true if it’s either strictly increasing or strictly decreasing, otherwise returns false.In Mathematics, a strictly increasing function is that function in which the value to be plotted always increases. Similarly, a strictly decreasing function is that function in which the value to be plotted always decreases.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [12, 45, 6, 4, 23, 23, 21, 1]; const arr2 = [12, 45, 67, 89, 123, 144, 2656, 5657]; const sameSlope ... Read More

Advertisements