Found 6683 Articles for Javascript

Appending suffix to numbers in JavaScript

AmitDiwan
Updated on 22-Apr-2021 11:29:35

857 Views

ProblemWe are required to write a JavaScript function that takes in a number, num, as the first and the only argument.The task of our function is to append ‘st’, ‘nd’, ‘rd’, ‘th’ to the number according to the following rules:st is used with numbers ending in 1 (e.g. 1st, pronounced first)nd is used with numbers ending in 2 (e.g. 92nd, pronounced ninety-second)rd is used with numbers ending in 3 (e.g. 33rd, pronounced thirty-third)As an exception to the above rules, all the "teen" numbers ending with 11, 12 or 13 use - th (e.g. 11th, pronounced eleventh, 112th, pronounced one hundred ... Read More

Adding binary without converting in JavaScript

AmitDiwan
Updated on 22-Apr-2021 11:28:03

197 Views

ProblemWe are required to write a JavaScript function that takes in two binary strings str1 and str2 as the first and the second argumentOur function should return the sum of the two binary numbers. We are not allowed to convert the binary numbers into decimal and then add and the resulting sum should contain no zeros at all.For example, if the input to the function is −Inputconst str1 = '1101'; const str2 = '10111';Outputconst output = '100100';ExampleFollowing is the code − Live Democonst str1 = '1101'; const str2 = '10111'; const addBinary = (str1 = '', str2 = '') => { ... Read More

Computing Ackerman number for inputs in JavaScript

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

259 Views

Ackermann FunctionThe Ackermann Function is a classic example of a recursive function, notable especially because it is not a primitive recursive function. It grows very quickly in value, as does the size of its call tree.ProblemWe are required to write a JavaScript function that takes in two numbers, m and n as the first and the second argument. Our function should return the Ackermann number A(m, n) defined byA(m, n) = n+1 if m=0 A(m, n) = A(m-1, 1) if m>0 , n=0 A(m, n) = A(m-1, A(m, n-1)) if m, n > 0Exampleconst m = 12; const n = ... Read More

ASCII to hex and hex to ASCII converter class in JavaScript

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

542 Views

ProblemWe are required to write a JavaScript class that have to member functions −toHex: It takes in a ASCII string and returns its hexadecimal equivalent.toASCII: It takes in a hexadecimal string and returns its ASCII equivalent.For example, if the input to the function is −Inputconst str = 'this is a string';Then the respective hex and ascii should be −74686973206973206120737472696e67 this is a stringExampleconst str = 'this is a string'; class Converter{    toASCII = (hex = '') => {       const res = [];       for(let i = 0; i < hex.length; i += 2){   ... Read More

Sorting one string by the order of second in JavaScript

AmitDiwan
Updated on 22-Apr-2021 11:14:29

154 Views

ProblemWe are required to write a JavaScript function that takes in two strings, str1 and str2 as the first and the second argument.Our function should sort str1 according to the order of characters as they appear in str2For example, if the input to the function is −Inputconst str1 = 'coding'; const str2 = 'gncabdi';Outputconst output = 'gncdio';Output ExplanationThe characters that appear first in str2 are placed first followed by the ones that comes later and lastly followed by the letters absent in str2.ExampleFollowing is the code − Live Democonst str1 = 'coding'; const str2 = 'gncabdi'; const sortByOrder = (str1 = ... Read More

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

422 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

Advertisements