Found 8591 Articles for Front End Technology

Checking semiprime numbers - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:11:12

391 Views

We are required to write a JavaScript function that takes in a number and the function establishes if the provided number is a semiprime or not.SemiprimeA semiprime number is that number which is a special type of composite number that is a product of two prime numbers. For example: 6, 15, 10, 77 are all semiprime. The square of a prime number is also semiprime, like 4, 9, 25 etc.ExampleFollowing is the code to check semi-prime numbers −const num = 141; const checkSemiprime = num => {    let cnt = 0;    for (let i = 2; cnt < ... Read More

Sorting digits of all the number of array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:10:00

783 Views

We are required to write a JavaScript function that takes in an array of numbers and reorders the digit of all the numbers internally in a specific order (lets say in ascending order for the sake of this problem).For example − If the array is −const arr = [543, 65, 343, 75, 567, 878, 87];Then the output should be −const output = [345, 56, 334, 57, 567, 788, 78];ExampleFollowing is the code −const arr = [543, 65, 343, 75, 567, 878, 87]; const ascendNumber = num => {    const numArr = String(num).split('').map(el => +el);    numArr.sort((a, b) => a ... Read More

Counting the number of redundant characters in a string - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:08:49

278 Views

We are required to write a JavaScript function that takes in a string and returns the count of redundant characters in the string.For example − If the string is −const str = 'abcde'Then the output should be 0If the string is −const str = 'aaacbfsc';Then the output should be 3ExampleFollowing is the code −const str = 'aaacbfsc'; const countRedundant = str => {    let count = 0;    for(let i = 0; i < str.length; i++){       if(i === str.lastIndexOf(str[i])){          continue;       };       count++;    };    return count; }; console.log(countRedundant(str));OutputFollowing is the output in the console −3

Checking progressive array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:07:43

205 Views

We are required to write a JavaScript function that takes in an array of strings, ordered by ascending length.The function should return true if, for each pair of consecutive strings, the second string can be formed from the first by adding a single letter either at the beginning or end.For example: If the array is given by −const arr = ["c", "ca", "can", "acan", "acane", "dacane"];Then our function should return true.ExampleFollowing is the code −const arr = ["c", "ca", "can", "acan", "acane", "dacane"]; const isProgressive = arr => {    for(let i = 0; i < arr.length-1; i++){     ... Read More

Returning poker pair cards - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:02:15

160 Views

We are required to write a function that takes in an array of exactly five elements representing the five cards of a poker player drawn randomly.If the five cards contain at least one pair, our function should return the card number of the highest pair (trivial if there only exists a single pair). Else our function should return false.For example: If the array is −const arr = ['A', 'Q', '3', 'A', 'Q'];Then our function should return −'A'  (as 'A' > 'Q' in card games)ExampleFollowing is the code −const arr = ['A', 'Q', '3', 'A', 'Q']; const greatestPair = arr => ... Read More

Finding closed loops in a number - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:00:51

485 Views

Other than all being a natural number, the numbers 0, 4, 6, 8, 9 have one more thing in common. All these numbers are formed by or contain at least one closed loop in their shapes.For example, the number 0 is a closed loop, 8 contains two closed loops and 4, 6, 9 each contains one closed loop.We are required to write a JavaScript function that takes in a number and returns the sum of the closed loops in all its digits.For example, if the number is 4789Then the output should be 4 i.e.1 + 0 + 2 + 1ExampleFollowing is ... Read More

Removing last vowel - JavaScript

AmitDiwan
Updated on 18-Sep-2020 08:59:33

367 Views

We are required to write a JavaScript function that takes in a string and returns a new string with the last vowel of each word removed.For example − If the string is −const str = 'This is an example string';Then the output should be −const output = 'Ths s n exampl strng';ExampleFollowing is the code −const str = 'This is an example string'; const removeLast = word => {    const lastIndex = el => word.lastIndexOf(el);    const ind = Math.max(lastIndex('a'), lastIndex('e'), lastIndex('i'),    lastIndex('o'), lastIndex('u'));    return word.substr(0, ind) + word.substr(ind+1, word.length); } const removeLastVowel = str => { ... Read More

Subtracting two numbers without using the (-) sign JavaScript

AmitDiwan
Updated on 18-Sep-2020 08:58:01

330 Views

We are required to write a JavaScript function that takes in two numbers and returns their difference but without using the (-) signExampleFollowing is the code −const num1 = 56; const num = 78; const subtractWithoutMinus = (num1, num2) => {    if(num2 === 0){       return num1;    };    return subtractWithoutMinus(num1 ^ num2, (~num1 & num2)

Finding unlike number in an array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 08:56:54

202 Views

We are required to write a JavaScript function that takes in an array of literals containing all similar elements but one. Our function should return the unlike number.ExampleFollowing is the code −const arr = [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]; // considering that the length of array is atleast 3 const findUnlike = arr => {    for(let i = 1; i < arr.length-1; i++){       if(arr[i] - arr[i-1] !== 0 && arr[i]-arr[i+1] === 0){          return arr[i-1];       }else if(arr[i] - arr[i-1] !== 0 && arr[i]-arr[i+1] === ... Read More

Nearest Prime to a number - JavaScript

AmitDiwan
Updated on 18-Sep-2020 08:55:41

532 Views

We are required to write a JavaScript function that takes in a number and returns the first prime number that appears after n.For example: If the number is 24,Then the output should be 29ExampleFollowing is the code −const num = 24; 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 nearestPrime = num => {    while(!isPrime(++num)){};    return num; }; console.log(nearestPrime(24));OutputFollowing is the output in the console −29

Advertisements