Create a Custom toLowerCase Function in JavaScript

AmitDiwan
Updated on 31-Aug-2020 12:25:49

436 Views

We are required to write a JavaScript String function that overwrite the default toLowerCase() and should have the same functionality as the default function.Let's write the code for this function −Exampleconst str = 'Some UpPerCAsE LeTTeRs!!!'; const toLowerCase = function(){    let str = '';    for(let i = 0; i < this.length; i++){       const ascii = this[i].charCodeAt();       if(ascii >= 65 && ascii

Segregate All 0s on Right and 1s on Left in JavaScript

AmitDiwan
Updated on 31-Aug-2020 12:24:26

221 Views

We have an array of Numbers that contains 0, 1 and some other numbers. We are required to write a JavaScript function that takes in this array and brings all 1s to the start and 0s to the endLet's write the code for this function −Exampleconst arr = [3, 2, 1, 8, 9, 0, 1, 9, 0, 2, 1, 0, 2, 0, 1, 0, 1, 1, 4, 0, 3]; const segregate = arr => {    const copy = arr.slice();    for(let i = 0; i < copy.length; i++){       if(copy[i] === 0){          copy.push(copy.splice(i, 1)[0]);       }else if(copy[i] === 1){          copy.unshift(copy.splice(i, 1)[0]);       };       continue;    };    return copy; }; console.log(segregate(arr));OutputThe output in the console will be −[    1, 1, 1, 3, 2, 8, 9,    1, 9, 2, 2, 1, 1, 4,    3, 0, 0, 0, 0, 0, 0 ]

Mersenne Prime in JavaScript

AmitDiwan
Updated on 31-Aug-2020 12:22:44

294 Views

In Mathematics, a Mersenne prime is a number that can be written in the form M(n) = 2^n − 1 for some integer n and is actually a prime number.For example − The first four Mersenne primes are 3, 7, 31, and 127We are required to write a JavaScript function that takes in a number and checks whether it is a Mersenne prime or not. Let’s write the code for this functionExampleconst isPrime = num => {    let i = 2;    while(i {    if(!isPrime(num)){       return false;    };    let i = 0, ... Read More

Check If String Starts With Another String in JavaScript

AmitDiwan
Updated on 31-Aug-2020 12:21:12

206 Views

We are required to write a JavaScript function that takes in two strings and checks whether first string starts with second or notFor example −If the two strings are: “Disaster management report” “Disas” Then our function should return trueLet's write the code for this function −Exampleconst first = 'The game is on'; const second = 'The'; const startsWith = (first, second) => {    const { length } = second;    const { length: l } = first;    const sub = first.substr(0, length);    return sub === second; }; console.log(startsWith(first, second));OutputThe output in the console will be −true

Leaders Array in JavaScript

AmitDiwan
Updated on 31-Aug-2020 12:19:45

359 Views

An element in an array of Numbers is a leader if it is greater than all the elements on its right side. We are required to write a JavaScript function that takes in an array of Numbers and returns a subarray of all the elements that are fulfil the criteria of being a leader element.For example −If the input array is: [23, 55, 2, 56, 3, 6, 7, 1] Then the output should be: [56, 7, 1]Let's write the code for this function −Exampleconst arr = [23, 55, 2, 56, 3, 6, 7, 1]; const leaderArray = arr => { ... Read More

Count Number of Subsequences with GCD 1 in C++

Sunidhi Bansal
Updated on 31-Aug-2020 11:56:01

380 Views

We are given an array of integer elements and the task is to find the sub-sequences from the given array which are having GCD as 1. GCD is the greatest common divisor of two or more integers that divides the given numbers entirely and greatest amongst all.Input − int arr[] = {3, 4, 8, 16}Output − Count of number of sub-sequences with GCD 1 are − 7Explanation −The sub-sequences that can be formed from the given array with GCD as 1 are (3, 4), (3, 8), (3, 16), (4, 3), (8, 3), (16, 3), (3, 4, 8)Input − int arr[] ... Read More

Count Number of Occurrences in a Sorted Array in C++

Sunidhi Bansal
Updated on 31-Aug-2020 11:51:01

2K+ Views

We are given a sorted array of integer type elements and the number let’s say, num and the task is to calculate the count of the number of times the given element num is appearing in an array.Input − int arr[] = {1, 1, 1, 2, 3, 4}, num = 1Output − Count of number of occurrences (or frequency) in a sorted array are − 3Input − int arr[] = {2, 3, 4, 5, 5, 6, -7}, num = 5Output − Count of number of occurrences (or frequency) in a sorted array are − 2Input − int arr[] = {-1, ... Read More

Count Rows and Columns with Sum Equal to Diagonal Sum in C++

Sunidhi Bansal
Updated on 31-Aug-2020 11:43:47

187 Views

We are given a matrix which is a 2-D array having rows and columns and the task is to calculate the count of sum of all the rows and columns such that it is equal to the sum of either principal or secondary matrix.Input −int arr[row][col] = {    { 4, 1, 7 },    { 10, 3, 5 },    { 2, 2, 11} }Output − Count of rows/columns with sum equals to diagonal sum are &mins; 2Explanation −sum of principal diagonal is: 4 + 3 + 11 = 18 and sum of secondary diagonal is: 7 + 3 ... Read More

Maximum Circular Subarray Sum in C++

Sunidhi Bansal
Updated on 31-Aug-2020 11:38:09

385 Views

We are given an array and the task is to form the subarrays such that the sum of subarrays in a circular fashion will result in a maximum value.Input − int arr[] = {1, 2, 8, 4, 3, 0, 7}Output − Maximum circular subarray sum is − 22Explanation − we are given an array containing {1, 2, 8, 4, 3, 0, 7} and the subarray of it with maximum sum will be 7 + 1 + 2+ 8 + 4 is 22.Input − int arr[] = { 2, 5, -1, 6, 9, 4, -5 }Output − Maximum circular subarray sum ... Read More

Count Smaller Values Whose XOR with X is Greater Than X in C++

Sunidhi Bansal
Updated on 31-Aug-2020 11:33:41

188 Views

We are given an integer number let’s say, x and the task are to count the smaller numbers less than x whose XOR with x will result in a value greater than the XOR value.The truth table for XOR operation is given belowABA XOR B000101011110Input − int x = 11Output − Count of smaller values whose XOR with x is greater than x are − 4Explanation −We are given with the x as 11 which means we need to find XOR of x with the numbers less than x. So the numbers are 1 XOR 11 < 11(FALSE), 2 XOR ... Read More

Advertisements