Javascript Articles

Page 376 of 534

Check for Subarray in the original array with 0 sum JavaScript

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 481 Views

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

Segregate all 0s on right and 1s on left in JavaScript

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 270 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 ]

Read More

Mersenne prime in JavaScript

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 340 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

Return TRUE if the first string starts with a specific second string JavaScript

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 242 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

Read More

Leaders array JavaScript

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 411 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

JavaScript Determine the array having majority element and return TRUE if its in the same array

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 196 Views

We are required to write a JavaScript function that takes in an array of numbers with repetitive values and returns the number that appears for more than (n/2) number of times where n is the length of the array. If there is no such element in the array, our function should return falseLet's write the code for this function −Exampleconst arr = [12, 5, 67, 12, 4, 12, 4, 12, 6, 12, 12]; const arr1 = [3, 565, 7, 23, 87, 23, 3, 65, 1, 3, 6, 7]; const findMajority = arr => {    let maxChar = -Infinity, maxCount ...

Read More

Check for Ugly number in JavaScript

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 1K+ Views

In the decimal number system, ugly numbers are those positive integers whose only prime factors are 2, 3 or 5.For example − Integers from 1 to 10 are all ugly numbers, 12 is an ugly number as well.Our job is to write a JavaScript function that takes in a Number and determines whether it is an ugly number or not.Let's write the code for this function −Exampleconst num = 274; const isUgly = num => {    while(num !== 1){       if(num % 2 === 0){          num /= 2;       } else ...

Read More

Reverse all the words of sentence JavaScript

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 4K+ Views

We are required to write a JavaScript function that takes in a string and returns a new string which has all the words reversed from the original string.For example −If the original string is −"Hello World how is it outside"Then the output should be −"olleH dlroW woH si ti edistuo"Now, let's write the code for this function −Exampleconst str = 'Hello World how is it outside'; const reverseSentence = str => {    const arr = str.split(" ");    const reversed = arr.map(el => {       return el.split('').reverse().join("");    });    return reversed.join(" "); }; console.log(reverseSentence(str));OutputThe output in ...

Read More

Remove all characters of first string from second JavaScript

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 416 Views

Let’s say, we have two strings that contains characters in no specific order. We are required to write a function that takes in these two strings and returns a modified version of the second string in which all the characters that were present in the first string are omitted.Following are our strings −const first = "hello world"; const second = "hey there";Following is our function to remove all characters of first string from second −const removeAll = (first, second) => {    const newArr = second.split("").filter(el => {       return !first.includes(el);    });    return newArr.join(""); };Let's write ...

Read More

Smallest positive value that cannot be represented as sum of subarray JavaScript

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 190 Views

We have a sorted array of positive integers like this −const arr = [1, 3, 6, 10, 11, 15];We are required to write a function, say findSmallest() that takes in one such array and returns the smallest positive integer that cannot be represented as the sum of some subarray of this original array.For example −For this array written above 2 is the smallest positive integer which cannot be reached by summing any subarray of this original array. So, now let's write the code for this function. As the array is sorted, we can achieve the solution to this problem in ...

Read More
Showing 3751–3760 of 5,338 articles
« Prev 1 374 375 376 377 378 534 Next »
Advertisements