We will be given an array of numbers / strings that contains some duplicate entries, all we have to do is to return the frequency of each element in the array.Returning an object with an element as key and its value as frequency would be perfect for this situation.We will iterate over the array with a forEach() loop and keep increasing the count of elements in the object if it already exists otherwise we will create a new property for that element in the object.And lastly, we will return the object.The full code for this problem will be −const arr ... Read More
We are required to write a function breakString() that takes in two arguments first the string to be broken and second is a number that represents the threshold count of characters after reaching which we have to repeatedly add line breaks in place of spaces.So, let’s do it. We will iterate over the with a for loop, we will keep a count that how many characters have elapsed with inserting a ‘’ if the count exceeds the limit and we encounter a space we replace it with line break in the new string and reset the count to 0 otherwise ... Read More
We have an array that contains some numbers and some strings, we are required to sort the array such that the numbers get sorted and get placed before every string and then the string should be placed sorted alphabetically.For exampleLet’s say this is our array −const arr = [1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9];The output should look like this −[1, 6, 7, 9, 47, 'afv', 'bdf', 'fdf', 'svd']Therefore, let’s write the code for this −const arr = [1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9]; const sorter = (a, b) => { if(typeof a === ... Read More
We are required to write a function that takes in a string as one and only argument and returns another string that has all ‘i’ and ‘o’ replaced with ‘1’ and ‘0’ respectively.It’s one of those classic for loop problems where we iterate over the string with its index and construct a new string as we move through.The code for the function will be −const string = 'Hello, is it raining in Amsterdam?'; const validate = (str) => { let validatedString = ''; for(let i = 0; i < str.length; i++){ if(str[i] === 'a'){ ... Read More
A number is a gapful number when −It has at least three digits, andIt is exactly divisible by the number formed by putting its first and last digits togetherFor example:1053 is a gapful number because it has 4 digits and it is exactly divisible by 13. 135 is a gapful number because it has 3 digits and it is exactly divisible by 15.Our job is to write a program that returns the nearest gapful number to the number we provide as input.Let’s write the code −const n = 134; //receives a number string and returns a boolean const isGapful = ... Read More
We are given a main string and a substring, our job is to create a function, let’s say removeString() that takes in these two arguments and returns a version of the main string which is free of the substring.Here, we need to remove the separator from a string, for example −this-is-a-stingLet’s now write the code for this function −const removeString = (string, separator) => { //we split the string and make it free of separator const separatedArray = string.split(separator); //we join the separatedArray with empty string const separatedString = separatedArray.join(""); return separatedString; } const str ... Read More
We are required to write a function removeStr() that lives on String.prototype object and takes in a string str, a character char and a number n.The function should remove the nth appearance of char from str.Let’s write the code for this −const str = 'aaaaaa'; const subStr = 'a'; const num = 6; removeStr = function(subStr, num){ if(!this.includes(subStr)){ return -1; } let start = 0, end = subStr.length; let occurences = 0; for(; ;end < this.length){ if(this.substring(start, end) === subStr){ occurences++; }; ... Read More
We are required to write a JavaScript function that takes in an array of numbers. The function should return a subarray of two adjacent elements from the original array whose sum is the least amongst all adjacent pairs of the array.If the length of the array is less than 2, we should return boolean false.For example, If the input array is −const arr = [41, 44, -12, 13, -23, 1, 5, -4, 2, 2];Here, the sum of pair [-23, 1] is -22 which is the least for any two adjacent elements of the array, so the function should return [-23, ... Read More
We are required to write a JavaScript function that takes in an array of numbers of length N such that N is a positive even integer and divides the array into two sub arrays (say, left and right) containing N/2 elements each.The function should do the product of the subarrays and then add both the results thus obtained.For example, If the input array is −const arr = [1, 2, 3, 4, 5, 6]Then the output should be −(1*2*3) + (4*5*6) 6+120 126The code for this will be −const arr = [1, 2, 3, 4, 5, 6] const subArrayProduct = arr ... Read More
Suppose, we have an array of numbers like this −const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14];We are required to write a JavaScript function that takes in one such array and counts the sum of all the elements of the array that appear only once in the array −For example:The output for the array mentioned above will be −356The code for this will be −const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14]; const nonRepeatingSum = arr => { let res = 0; for(let i = 0; i < arr.length; i++){ if(i !== arr.lastIndexOf(arr[i])){ continue; }; res += arr[i]; }; return res; }; console.log(nonRepeatingSum(arr));Following is the output on console −30
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP