We have an array of arrays that contains the marks scored by some students in some subjects −const arr = [ ['Math', 'John', 100], ['Math', 'Jake', 89], ['Math', 'Amy', 93], ['Science', 'Jake', 89], ['Science', 'John', 89], ['Science', 'Amy', 83], ['English', 'John', 82], ['English', 'Amy', 81], ['English', 'Jake', 72] ];We are required to write a function that takes in this array and retuns an array of object, with one object for each subject and the details about the top scorer of that subject.Our output should look like −[ { "Subject": "Math", ... Read More
We are required to write a JavaScript function that takes in a string and a number n as two arguments (the number should be such that it exactly divides the length of string). And we have to return an array of n strings of equal length.For example −If the string is "helloo" and the number is 3 Our output should be: ["ho", "eo", "ll"]Here, each substring exactly contains (length of array/n) characters. And each substring is formed by taking corresponding first and last letters of the string alternativelyLet's write the code for this function −Exampleconst str = 'helloo'; const splitEqual ... Read More
We are required to write a JavaScript function that takes in an array of Numbers with some positive and negative values. We are required to determine whether there exists a subarray in the original array whose net sum is 0 or not.Our function should return a boolean on this basis.ApproachThe approach here is simple. We iterate over the array using a for loop, calculate the cumulative sum up to that particular element. And if any point the cumulative becomes 0 or attains a value it has previously attained, then there exists a subarray with sum 0. Otherwise there exists no ... Read More
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
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 ]
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
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
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
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
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
 Data Structure
 Networking
 RDBMS
 Operating System
 Java
 iOS
 HTML
 CSS
 Android
 Python
 C Programming
 C++
 C#
 MongoDB
 MySQL
 Javascript
 PHP