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
We are required to write a JavaScript function that takes in a number and checks whether it is a Fibonacci number or not (i.e., it falls in Fibonacci series or not).Our function should return true if the number is a Fibonacci number, false otherwise.The code for this will be −const num = 2584; const isFibonacci = num => { if(num === 0 || num === 1){ return true; } let prev = 1; let count = 2; let temp = 0; while(count
Suppose, we have a sorted array of literals like this −const arr = [32, 32, 63, 63, 63, 75, 75, 86, 87, 88, 89];We are required to write a JavaScript function that takes in one such array and returns the first unique number in the array.If there is no such number in the array, our function should return false.For this array, the output should be 86.The code for this will be −const arr = [32, 32, 63, 63, 63, 75, 75, 86, 87, 88, 89]; const firstUnique = arr => { let appeared = false; for(let i = ... Read More
We are required to write a JavaScript function that takes in an array of Numbers and returns the smallest number from it using recursion.Let’s say the following are our arrays −const arr1 = [-2, -3, -4, -5, -6, -7, -8]; const arr2 = [-2, 5, 3, 0];The code for this will be −const arr1 = [-2, -3, -4, -5, -6, -7, -8]; const arr2 = [-2, 5, 3, 0]; const min = arr => { const helper = (a, ...res) => { if (!res.length){ return a; }; ... Read More
We are required to write a JavaScript function that takes in a number and returns an English count number for it.For example 3 returns 3rdThe code for this will be −const num = 3; const englishCount = num => { if (num % 10 === 1 && num % 100 !== 11){ return num + "st"; }; if (num % 10 === 2 && num % 100 !== 12) { return num + "nd"; }; if (num % 10 === 3 && num % 100 !== 13) { return num + "rd"; }; return num + "th"; }; console.log(englishCount(num)); console.log(englishCount(111)); console.log(englishCount(65)); console.log(englishCount(767));Following is the output on console −3rd 111th 65th 767th