Found 10710 Articles for Web Development

Finding length of repeating decimal part in JavaScript

AmitDiwan
Updated on 22-Apr-2021 11:10:41

244 Views

ProblemWe are required to write a JavaScript function that takes in a number, num, as the first and the only argumentOur function should do two thingsFirst of all, it should check whether the number is prime with 10 or not, if its not, we should return -1 (a number is prime with any other number if the only common factor they share is 1).If the number is prime with 10, then we should return the length of the decimal part which repeats itself, when that very number divides 1.For example, if the input to the function is −Inputconst num = ... Read More

Returning just greater array in JavaScript

AmitDiwan
Updated on 22-Apr-2021 11:09:42

59 Views

ProblemWe are required to write a JavaScript function that takes in an array of positive integers, arr, as the first and the only argument.Our function should first join the numbers present in the array and find the single number represented by the array and then return a new array that represents the number which is greater than the input array number by a magnitude of 1.For example, if the input to the function is −Inputconst arr = [6, 7, 3, 9];Outputconst output = [6, 7, 4, 0];Output ExplanationBecause the number represented by input array is 6739 and the required number ... Read More

Converting alphabets to Greek letters in JavaScript

AmitDiwan
Updated on 22-Apr-2021 11:08:13

424 Views

ProblemWe are required to write a JavaScript function that takes in a string of uppercase English alphabets, str, as the first and the only argument.Consider the following mapping between English and Greek letters −A=α (Alpha) B=β (Beta) D=δ (Delta) E=ε (Epsilon) I=ι (Iota) K=κ (Kappa) N=η (Eta) O=θ (Theta) P=ρ (Rho) R=π (Pi) T=τ (Tau) U=μ (Mu) V=υ (Upsilon) W=ω (Omega) X=χ (Chi) Y=γ (Gamma)For all the alphabets that have a Greek mapping our function should create a new string in which the English letter is replaced by corresponding Greek letter and if there exists no mapping we should persist ... Read More

Returning only odd number from array in JavaScript

AmitDiwan
Updated on 22-Apr-2021 11:04:20

506 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers, arr, as the first and the only argument.The array either consists of all even numbers and just one odd number or consists of all odd numbers and just one even number. Our function should return this one different element from the array.For example, if the input to the function is −Inputconst arr = [5, 9, 7, 11, 34, 23, 77];Outputconst output = 34;Output ExplanationBecause the array consists of all odd numbers but 34 which is even.ExampleFollowing is the code − Live Democonst arr = [5, 9, ... Read More

Counting possible APs within an array in JavaScript

AmitDiwan
Updated on 22-Apr-2021 10:59:48

84 Views

Arithmetic ProgressionArithmetic Progression (AP) is a sequence of numbers such that the difference of any two consecutive numbers is a constant value (aka common difference).For instance, 1, 2, 3, 4, 5, 6, … is an AP, which has a common difference equal to 1 (2 -1).ProblemWe are required to write a JavaScript function that takes in an array of integers, arr, as the first and the only argument.The task of our function is to return the number of arithmetic progressions of size 3 that are possible from that list. In each progression, the differences between the elements must be the ... Read More

Returning number of digits in factorial of a number in JavaScript

AmitDiwan
Updated on 22-Apr-2021 10:58:25

110 Views

ProblemWe are required to write a JavaScript function that takes in a number, num, as the first and the only argument.Our function should compute and return the number of digits in the factorial of the number num.For example, if the input to the function is −Inputconst num = 7;Outputconst output = 4;Output ExplanationBecause the value of 7! Is 5040 which contains 4 digits.ExampleFollowing is the code − Live Democonst num = 7; const countDigits = (num = 1) => {    let res = 0;    while(num >= 2){       res += Math.log10(num);       num--;    };    return ~~res + 1; } console.log(countDigits(num));Output4

Array index to balance sums in JavaScript

AmitDiwan
Updated on 22-Apr-2021 10:55:13

440 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers, arr, as the first and the only argument.Our function is required to pick and return one such index from the array such that the sum of elements on its left side is equal to the sum of elements on its right side. If there exists no such index in the array, we should return -1.For example, if the input to the function is −Inputconst arr = [1, 2, 3, 4, 3, 2, 1];Outputconst output = 3;Output ExplanationBecause the sum of elements at either side of ... Read More

Encoding string based on character frequency in JavaScript

AmitDiwan
Updated on 22-Apr-2021 10:50:30

127 Views

ProblemWe are required to write a JavaScript function that takes in a string, str, as the first and the only argument.Our function should create a new string based on the input string where each character in the new string is '(' if that character appears only once in the original string, or ')' if that character appears more than once in the original string.And we should ignore capitaliFor example, if the input to the function is −Inputconst str = 'Success';Outputconst output = ')())())';ExampleFollowing is the code − Live Democonst str = 'Success'; const mapString = (str = '') => {   ... Read More

Limiting elements occurrences to n times in JavaScript

AmitDiwan
Updated on 22-Apr-2021 10:44:53

160 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers, arr, that may contain duplicates as the first argument, and a number, num, as the second and final argument.The task of our function is to iterate through the array and check whether there exists some number that appears for more than n times in the array.If there exists any such element, we should delete its extra occurrences to limit its occurrence to at most num.For example, if the input to the function is −Inputconst arr = [4, 1, 3, 1, 4, 1, 3, 4, 2]; ... Read More

Converting array to phone number string in JavaScript

AmitDiwan
Updated on 22-Apr-2021 10:39:16

1K+ Views

ProblemWe are required to write a JavaScript function that takes in an array of exactly 10 positive integers, arr, as the first and the only argument.Our function should then return a string which is in the format of a phone number string.For example, if the input to the function is −Inputconst arr = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0];Outputconst output = '(987) 654-3210';ExampleFollowing is the code − Live Democonst arr = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]; const createNumber = (arr = []) => {    let res = '';    arr = arr.map(String);    res += `(${arr[0]+arr[1]+arr[2]}) `;    res += `${arr[3] + arr[4] + arr[5]}-`;    res += arr[6] + arr[7] + arr[8] + arr[9];    return res; }; console.log(createNumber(arr));Output(987) 654-3210

Advertisements