Found 10483 Articles for Web Development

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

329 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

Checking the intensity of shuffle of an array - JavaScript

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

179 Views

An array of numbers is 100% shuffled if no two consecutive numbers appear together in the array (we only consider the ascending order case here). And it is 0% shuffled if pairs are of consecutive numbers.For an array of length n there will be n-1 pairs of elements (without distorting its order).We are required to write a JavaScript function that takes in an array of numbers and returns a number between [0, 100] representing the intensity of shuffle in the arrayExampleFollowing is the code −const arr = [4, 23, 1, 23, 35, 78, 4, 45, 7, 34, 7]; // this function calculates deviation from ascending sort const shuffleIntensity = arr => {    let inCorrectPairs = 0;    if(arr.length

Random name generator function in JavaScript

AmitDiwan
Updated on 18-Sep-2020 08:53:10

5K+ Views

We are required to write a JavaScript function that takes in a number n and returns a random string of length n containing no other than the 26 English lowercase alphabetsExampleLet us write the code for this function −const num = 8; const randomNameGenerator = num => {    let res = '';    for(let i = 0; i < num; i++){       const random = Math.floor(Math.random() * 27);       res += String.fromCharCode(97 + random);    };    return res; }; console.log(randomNameGenerator(num));OutputFollowing is the output in the console −kdcwpingNote − This is one of many possible outputs. Console output is expected to differ every time/

Finding the nth day from today - JavaScript (JS Date)

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

461 Views

We are required to write a JavaScript function that takes in a number n as the only input.The function should first find the current day (using Date Object in JavaScript) and then the function should return the day n days from today.For example −If today is Monday and n = 2, Then the output should be −WednesdayExampleFollowing is the code −const num = 15; const findNthDay = num => {    const weekday=new Array(7);    weekday[1]="Monday";    weekday[2]="Tuesday";    weekday[3]="Wednesday";    weekday[4]="Thursday";    weekday[5]="Friday";    weekday[6]="Saturday";    weekday[7]="Sunday"    const day = new Date().getDay();    const daysFromNow = num % ... Read More

Beginning and end pairs in array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 08:51:03

131 Views

We are required to write a JavaScript function that takes in an array of Number / String literals and returns another array of arrays. With each subarray containing exactly two elements, the nth element from start nth from last.For example −If the array is −const arr = [1, 2, 3, 4, 5, 6];Then the output should be −const output = [[1, 6], [2, 5], [3, 4]];ExampleFollowing is the code −const arr = [1, 2, 3, 4, 5, 6]; const edgePairs = arr => {    const res = [];    const upto = arr.length % 2 === 0 ? arr.length ... Read More

Advertisements