Found 9150 Articles for Object Oriented Programming

Upper or lower elements count in an array in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:37:33

113 Views

Consider we have an array of Numbers that looks like this −const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78];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 60 −The answer should be 5 elements below it (54, 54, 43, 3, 23) and 5 element par it (65, 73, 78, 76, 78) Therefore, let’s write the code for this function −ExampleThe code for this will be −const array = [54, 54, 65, 73, 43, ... Read More

Finding sum of all unique elements in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:33:55

191 Views

We are required to write a JavaScript function that takes in an array of numbers with duplicate entries and sums all the duplicate entries to one index.For example: If the input array is −const input = [1, 3, 1, 3, 5, 7, 5, 4];OutputThen the output should be −const output = [2, 6, 7, 10, 4];// all the duplicate ones are summed to index 0// all the duplicate threes are summed to index 1 and so on.Therefore, let’s write the code for this function −ExampleThe code for this will be −const input = [1, 3, 1, 3, 5, 7, 5, ... Read More

Similarities between different strings in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:56:30

125 Views

We have two arrays of Numbers, and we are required to write a function intersection() that computes their intersection and returns an array that contains the intersecting elements in any order. Each element in the result should appear as many times as it shows in both arrays.For example:If input is −arr1 = ['hello', 'world', 'how', 'are', 'you']; arr2 = ['hey', 'world', 'can', 'you', 'rotate'];Then the output should be −['world', 'you'];Approach:Had the arrays been sorted, we could have used the two-pointer approach with initially both pointing to 0 the start of the respective array and we could have proceeded with increasing ... Read More

Grouping of same kind of numbers in JavaScript

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

169 Views

We have an array of numbers like this −const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0];We are required to write a JavaScript function that counts the consecutive groups of nonnegative (positives and 0) numbers in the array.Like here we have consecutive non negatives from index 3 to 3 (only one element, but still a cluster) which forms one group and then from 9 to end of array forms the second group.So, for this array, the function should return 2.Therefore, let’s write the code for this function −ExampleThe code for this will be −const ... Read More

Finding pandigital numbers using JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:49:16

187 Views

We are required to write a JavaScript function that takes in a string representing a number. The function returns true if the number is pandigital, false otherwise.A pandigital number is a number that contains all digits (0-9) at least once.Therefore, let’s write the code for this function −ExampleThe code for this will be −const numStr1 = '47458892414'; const numStr2 = '53657687691428890'; const isPandigital = numStr => {    let legend = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];    for(let i = 0; i < numStr.length; i++){       if(!legend.includes(numStr[i])){          continue;   ... Read More

Twice join of two strings in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:47:42

147 Views

We are required to write a JavaScript function that takes in two strings; creates and returns a new string with first 2 words of first string, next two words of second string, then first, then second and so on.For example: If the strings are −const str1 = 'Hello world'; const str2 = 'How are you btw';Then the output should be −const output = 'HeHollw o arwoe rlyodu btw';Therefore, let’s write the code for this function −ExampleThe code for this will be −const str1 = 'Hello world'; const str2 = 'How are you btw'; const twiceJoin = (str1 = '', str2 ... Read More

Finding the second most frequent character in JavaScript

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

282 Views

We are required to write a JavaScript function that takes in a string and returns the character which makes second most appearances in the string.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'Hello world, I have never seen such a beautiful weather in the world'; const secondFrequent = str => {    const map = {};    for(let i = 0; i < str.length; i++){       map[str[i]] = (map[str[i]] || 0) + 1;    };    const freqArr = Object.keys(map).map(el => [el, map[el]]);    freqArr.sort((a, b) => b[1] - ... Read More

Constructing array from string unique characters in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:43:20

144 Views

We are required to write a JavaScript function that takes in a string and starts mapping its characters from 0.And every time, the function encounters a unique (non-duplicate) character it should increase the mapping count by 1 otherwise it should map the same number for duplicate characters.For example: If the string is −const str = 'heeeyyyy';Then the output should be −const output = [0, 1, 1, 1, 2, 2, 2, 2];Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'heeeyyyy'; const mapString = str => {    const res = [];   ... Read More

Sorting the numbers from within in JavaScript

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

148 Views

We are required to write a JavaScript function that takes in an array of numbers and reorders the digit of all the numbers internally in a specific order (let’s say in ascending order for the sake of this problem).For example: If the array is −const arr = [543, 65, 343, 75, 567, 878, 87];Then the output should be −const output = [345, 56, 334, 57, 567, 788, 78];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [543, 65, 343, 75, 567, 878, 87]; const ascendNumber = num => {    const numArr ... Read More

Number of non-unique characters in a string in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:39:20

221 Views

We are required to write a JavaScript function that takes in a string and returns the count of redundant characters in the string.For example: If the string is −const str = 'abcde'Then the output should be 0.If the string is −const str = 'aaacbfsc';Then the output should be 3.ExampleThe code for this will be −const str = 'aaacbfsc'; const countRedundant = str => {    let count = 0;    for(let i = 0; i < str.length; i++){       if(i === str.lastIndexOf(str[i])){          continue;       };       count++;    };    return count; }; console.log(countRedundant(str));OutputThe output in the console will be −3

Advertisements