Found 8591 Articles for Front End Technology

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

Find the primorial of numbers - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:41:24

331 Views

The primorial of a number n is equal to the product of first n prime numbers.For example, if n = 4Then, the output primorial(n) is, 2*3*5*7 = 210We are required to write a JavaScript function that takes in a number and returns its primordial.ExampleFollowing is the code −const num = 4; 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){       ... Read More

Count the number of data types in an array - JavaScript

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

228 Views

We are required to write a JavaScript function that takes in an array that contains elements of different data types and the function should return a map representing the frequency of each data type.Let’s say the following is our array −const arr = [23, 'df', undefined, null, 12, {    name: 'Rajesh' }, [2, 4, 7], 'dfd', null, Symbol('*'), 8];ExampleFollowing is the code −const arr = [23, 'df', undefined, null, 12, {    name: 'Rajesh'},    [2, 4, 7], 'dfd', null, Symbol('*'), 8]; const countDataTypes = arr => {    return arr.reduce((acc, val) => {       const dataType ... Read More

Checking if two arrays can form a sequence - JavaScript

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

205 Views

We are required to write a JavaScript function that takes in two arrays of numbers.And the function should return true if the two arrays upon combining and shuffling can form a consecutive sequence, false otherwise.For example − If the arrays are −const arr1 = [4, 6, 2, 9, 3]; const arr2 = [1, 5, 8, 7];Then the output should be true.ExampleFollowing is the code −const arr2 = [1, 5, 8, 7]; const canFormSequence = (arr1, arr2) => {    const combined = [...arr1, ...arr2];    if(combined.length < 2){       return true;    };    combined.sort((a, b) => a-b); ... Read More

Finding second smallest word in a string - JavaScript

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

310 Views

We are required to write a JavaScript function that takes in a string sentence as first and the only argument. The function should return the length of the second smallest word from the string.For example: If the string is −const str = 'This is a sample string';Then the output should be 2.ExampleFollowing is the code −const str = 'This is a sample string'; const secondSmallest = str => {    const strArr = str.split(' ');    if(strArr.length < 2){       return false;    }    for(let i = 0; i < strArr.length; i++){       strArr[i] = ... Read More

Expressing numbers in expanded form - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:32:51

827 Views

Suppose we are given a number 124 and are required to write a function that takes this number as input and returns its expanded form as a string.The expanded form of 124 is −'100+20+4'ExampleFollowing is the code −const num = 125; const expandedForm = num => {    const numStr = String(num);    let res = '';    for(let i = 0; i < numStr.length; i++){       const placeValue = +(numStr[i]) * Math.pow(10, (numStr.length - 1 - i));       if(numStr.length - i > 1){          res += `${placeValue}+`       }else{          res += placeValue;       };    };    return res; }; console.log(expandedForm(num));OutputFollowing is the output in the console −100+20+5

Replacing every nth instance of characters in a string - JavaScript

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

972 Views

We are required to write a JavaScript function that takes in a string as the first argument, a number, say n, as the second argument and a character, say c, as the third argument. The function should replace the nth appearance of any character with the character provided as the third argument and return the new string.ExampleFollowing is the code −const str = 'This is a sample string'; const num = 2; const char = '*'; const replaceNthAppearance = (str, num, char) => {    const creds = str.split('').reduce((acc, val, ind, arr) => {       let { res, ... Read More

Advertisements