AmitDiwan has Published 10744 Articles

Subtracting two numbers without using the (-) sign JavaScript

AmitDiwan

AmitDiwan

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

327 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

AmitDiwan

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

201 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 ... Read More

Nearest Prime to a number - JavaScript

AmitDiwan

AmitDiwan

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

531 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 ... Read More

Checking the intensity of shuffle of an array - JavaScript

AmitDiwan

AmitDiwan

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

178 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 ... Read More

Random name generator function in JavaScript

AmitDiwan

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 ... Read More

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

AmitDiwan

AmitDiwan

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

459 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 = ... Read More

Beginning and end pairs in array - JavaScript

AmitDiwan

AmitDiwan

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

130 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, ... Read More

Find the primorial of numbers - JavaScript

AmitDiwan

AmitDiwan

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

328 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 = ... Read More

Count the number of data types in an array - JavaScript

AmitDiwan

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, {   ... Read More

Checking if two arrays can form a sequence - JavaScript

AmitDiwan

AmitDiwan

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

204 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 ... Read More

Advertisements