Found 8591 Articles for Front End Technology

Sum of prime numbers between a range - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:14:44

514 Views

We are required to write a JavaScript function that takes in two numbers, say a and b and returns the sum of all the prime numbers that fall between a and b. We should include a and b if they are prime as well.ExampleFollowing is the code −const num1 = 45; const num2 = 345; const isPrime = n => {    if (n===1){       return false;    }else if(n === 2){       return true;    }else{       for(let x = 2; x < n; x++){          if(n % x === 0){             return false;          }       }       return true;    }; }; const primeBetween = (a, b) => {    const res = [];    while(a

Finding unique string in an array in JavaScript

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

414 Views

Suppose, we have the following array of strings that might contain duplicate characters −const arr = ['54gdgdfe3', '434ffd', '43frdf', '43fdhnh', 'wgcxhjny', 'fsdf34'];We are required to write a JavaScript function that takes in one such array and returns the very first element from the array that contains 0 duplicate characters.If there does not exist any such string, we should return false.ExampleFollowing is the code −const arr = ['54gdgdfe3', '434ffd', '43frdf', '43fdhnh', 'wgcxhjny', 'fsdf34']; const isUnique = str => {    return str.split('').every(el => str.indexOf(el) ===    str.lastIndexOf(el)); }; const findUniqueString = arr => {    for(let i = 0; i < ... Read More

Odd even sort in an array - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:10:43

1K+ Views

We are required to write a JavaScript function that takes in an array of numbers and sorts the array such that first all the even numbers appear in ascending order and then all the odd numbers appear in ascending order.For example: If the input array is −const arr = [2, 5, 2, 6, 7, 1, 8, 9];Then the output should be −const output = [2, 2, 6, 8, 1, 5, 7, 9];ExampleFollowing is the code −const arr = [2, 5, 2, 6, 7, 1, 8, 9]; const isEven = num => num % 2 === 0; const sorter = ((a, ... Read More

Finding missing letter in a string - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:09:15

691 Views

We have a string of length m that contains first m letters of the English alphabets, but somehow, one element went missing from the string. So, now the string contains, m-1 lettersWe are required to write a function that takes in one such string and returns the missing element from the stringExampleFollowing is the code −const str = "acdghfbekj"; const missingCharacter = str => {    // to make the function more consistent    const s = str.toLowerCase();    for(let i = 97; ; i++){       if(s.includes(String.fromCharCode(i))){          continue;       };     ... Read More

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

Advertisements