Found 10483 Articles for Web Development

Repeating letter string - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:07:56

1K+ Views

We are required to write a JavaScript function that takes in a string and a number, say n, and the function should return a new string in which all the letters of the original string are repeated n times.For example: If the string is −const str = 'how are you'And the number n is 2Then the output should be −const output = 'hhooww  aarree  yyoouu'ExampleFollowing is the code −const str = 'how are you'; const repeatNTimes = (str, n) => {    let res = '';    for(let i = 0; i < str.length; i++){       // using ... Read More

Change every letter to next letter - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:06:46

726 Views

We are required to write a JavaScript function that takes in a string and changes every letter of the string from the English alphabets to its succeeding element.For example: If the string is −const str = 'how are you';Then the output should be −const output = 'ipx bsf zpv'ExampleFollowing is the code −const str = 'how are you'; const isAlpha = code => (code >= 65 && code = 97 && code code === 90 || code === 122; const nextLetterString = str => {    const strArr = str.split('');    return strArr.reduce((acc, val) => {       ... Read More

Absolute sum of array elements - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:05:34

798 Views

We are required to write a JavaScript function that takes in an array with both positive and negative numbers and returns the absolute sum of all the elements of the array.We are required to do this without taking help of any inbuilt library function.For example: If the array is −const arr = [1, -5, -34, -5, 2, 5, 6];Then the output should be −58ExampleFollowing is the code −const arr = [1, -5, -34, -5, 2, 5, 6]; const absoluteSum = arr => {    let res = 0;    for(let i = 0; i < arr.length; i++){       ... Read More

Converting string to a binary string - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:04:13

656 Views

We are required to write a JavaScript function that takes in a lowercase string and returns a new string in which all the elements between [a, m] are represented by 0 and all the elements between [n, z] are represented by 1.ExampleFollowing is the code −const str = 'Hello worlld how are you'; const stringToBinary = (str = '') => {    const s = str.toLowerCase();    let res = '';    for(let i = 0; i < s.length; i++){       // for special characters       if(s[i].toLowerCase() === s[i].toUpperCase()){          res += s[i]; ... Read More

Finding greatest digit by recursion - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:02:46

316 Views

We are required to write a JavaScript recursive function that takes in a number and returns the greatest digit in the number.For example: If the number is −45654356Then the return value should be 6ExampleFollowing is the code −const num = 45654356; const greatestDigit = (num = 0, greatest = 0) => {    if(num){       const max = Math.max(num % 10, greatest);       return greatestDigit(Math.floor(num / 10), max);    };    return greatest; }; console.log(greatestDigit(num));OutputFollowing is the output in the console −6

Calculating resistance of n devices - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:01:45

224 Views

In Physics, the equivalent resistance of say 3 resistors connected in series is given by −R = R1 + R2 + R3And the equivalent resistance of resistors connected in parallel is given by −R = (1/R1) + (1/R2) + (1/R3)We are required to write a JavaScript function that takes a string having two possible values, 'series' or 'parallel' and then n numbers representing the resistance of n resistors.And the function should return the equivalent resistance of these resistors.ExampleLet us write the code for this function.const r1 = 5, r2 = 7, r3 = 9; const equivalentResistance = (combination = 'parallel', ... Read More

Distance to nearest vowel in a string - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:00:07

462 Views

We are required to write a JavaScript function that takes in a string with atleast one vowel, and for each character in the string we have to map a number in a string representing its nearest distance from a vowel.For example: If the string is −const str = 'vatghvf';Then the output should be −const output = [1, 0, 1, 2, 3, 4, 5];ExampleFollowing is the code −const str = 'vatghvf'; const nearest = (arr = [], el) => arr.reduce((acc, val) => Math.min(acc, Math.abs(val - el)), Infinity); const vowelNearestDistance = (str = '') => {    const s = str.toLowerCase();   ... Read More

Valid triangle edges - JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:58:25

471 Views

Suppose, we have three lines of length, respectively l, m and n. These three lines can only form a triangle, if the sum of any arbitrary two sides is greater than the third side.For example, if the length of three lines is 4, 9 and 3, they cannot form a triangle because 4+3 is less than 9.We are required to write a JavaScript function that the three numbers represents the length of three sides and checks whether they can form a triangle or not.ExampleFollowing is the code −const a = 9, b = 5, c = 3; const isValidTriangle = ... Read More

Casting a string to snake case - JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:57:22

2K+ Views

Snake case is basically a style of writing strings by replacing the spaces with '_' and converting the first letter of each word to lowercase.We are required to write a JavaScript function that takes in a string and converts it to snake case.ExampleFollowing is the code −const str = 'This is a simple sentence'; const toSnakeCase = (str = '') => {    const strArr = str.split(' ');    const snakeArr = strArr.reduce((acc, val) => {       return acc.concat(val.toLowerCase());    }, []);    return snakeArr.join('_'); }; console.log(toSnakeCase(str));OutputFollowing is the output in the console −this_is_a_simple_sentence

Return index of first repeating character in a string - JavaScript

AmitDiwan
Updated on 16-Sep-2020 09:56:20

218 Views

We are required to write a JavaScript function that takes in a string and returns the index of first character that appears twice in the string.If there is no such character then we should return -1. Following is our string −const str = 'Hello world, how are you';ExampleFollowing is the code −const str = 'Hello world, how are you'; const firstRepeating = str => {    const map = new Map();    for(let i = 0; i < str.length; i++){       if(map.has(str[i])){          return map.get(str[i]);       };       map.set(str[i], i);   ... Read More

Advertisements