Found 10710 Articles for Web Development

Alternatingly combining array elements in JavaScript

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

56 Views

ProblemWe are required to write a JavaScript function that takes in any number of arrays of literals as input.Our function should prepare a new array that contains elements picked alternatingly from all the input arrays.For example, if the input to the function is −Inputconst arr1 = [1, 2, 3, 4]; const arr2 = [11, 12, 13, 14]; const arr3 = ['a', 'b', 'c'];Outputconst output = [1, 11, 'a', 2, 12, 'b', 3, 13, 'c', 4, 14];ExampleFollowing is the code − Live Democonst arr1 = [1, 2, 3, 4]; const arr2 = [11, 12, 13, 14]; const arr3 = ['a', 'b', 'c']; ... Read More

Arranging lexicographically and removing whitespaces in JavaScript

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

78 Views

ProblemWe are required to write a JavaScript function that takes in a string, str, that contains alphabets and whitespacesOur function should iterate over the input string and perform actions so that the characters are concatenated into a new string in "case-insensitively-alphabetical-order-of-appearance" order. Whitespace and punctuation shall simply be removed!For example, if the input to the function is −Inputconst str = 'some simple letter combination!';Outputconst output = 'abceeeeiiillmmmnnoooprssttt';ExampleFollowing is the code − Live Democonst str = 'some simple letter combination!'; const orderString = (str = '') => {    let res = '';       for(let i = 97; i < ... Read More

Crack Alphabets fight problem in JavaScript

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

74 Views

ProblemConsider a situation where armies of two bunch of alphabets are fighting each other. The soldiers of both and their weight are as follows −TeamASoldierWeightA1B2C3D4TeamBSoldierWeightW1X2Y3Z4Other than the soldiers, there are bombs as well in the arena denoted by ‘!’, and a bomb kills soldiers placed at its adjacent sides.For instance: ‘A!BC’ will result in ‘C’ and ‘!!CC!!’ will result in ‘’.Our function should need to find out when all the bombs in the arena explode which team wins or if both the teams end up with the same weight.For example, if the input to the function is −Inputconst str = ... Read More

Splitting number into n parts close to each other in JavaScript

AmitDiwan
Updated on 22-Apr-2021 11:34:55

909 Views

ProblemWe are required to write a JavaScript function that takes in a number, num, as the first argument and another number, parts, as the second argument.Our function should split the number num into exactly (parts) numbers and we should keep these two conditions in mind −The numbers should be as close as possibleThe numbers should even (if possible).And the ordering of numbers is not important.For example, if the input to the function is −Inputconst num = 20; const parts = 6;Outputconst output = [3, 3, 3, 3, 4, 4];ExampleFollowing is the code − Live Democonst num = 20; const parts = ... Read More

Counting adjacent pairs of words in JavaScript

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

225 Views

ProblemWe are required to write a JavaScript function that takes in a string str that represents a sentence as the only argument.Our function should count and return the adjacent pair of identical words present in the string str. Our function should check the words ignoring their case, which means ‘it’ and ‘It’ should be counted as identical.For example, if the input to the function is −Inputconst str = 'This this is a a sample string';Outputconst output = 2;Output ExplanationBecause the repeating words are ‘this’ and ‘a’.ExampleFollowing is the code − Live Democonst str = 'This this is a a sample string'; ... Read More

Appending suffix to numbers in JavaScript

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

865 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

198 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

262 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

550 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

Advertisements