Found 9150 Articles for Object Oriented Programming

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

Return TRUE if the first string starts with a specific second string 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 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

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

AmitDiwan
Updated on 31-Aug-2020 07:09:46

156 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

Equality of two arrays JavaScript

AmitDiwan
Updated on 31-Aug-2020 07:03:35

370 Views

We are required to write a JavaScript function that takes in two arrays of Numbers, say first and second and checks for their equality.Equality in our case will be determined by one of these two conditions −The arrays are equal if they contain the same elements irrespective of their order.If the sum of all the elements of the first array and second array is equal.For example −[3, 5, 6, 7, 7] and [7, 5, 3, 7, 6] are equal arrays [1, 2, 3, 1, 2] and [7, 2] are also equal arrays but [3, 4, 2, 5] and [2, 3, ... Read More

JavaScript Check whether string1 ends with strings2 or not

AmitDiwan
Updated on 31-Aug-2020 07:00:38

119 Views

We are required to write a JavaScript function that takes in two strings, say, string1 and string2 and determines whether the string1 ends with string2 or not.For example −"The game is on" Here, "on" should return trueWhile, "the game is off" Above, “of" should return falseLet's write the code for this function −Exampleconst first = 'The game is on'; const second = ' on'; const endsWith = (first, second) => {    const { length } = second;    const { length: l } = first;    const sub = first.substr(l - length, length);    return sub === second; }; ... Read More

Check for Ugly number in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:59:27

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

JavaScript Remove non-duplicate characters from string

Disha Verma
Updated on 18-Mar-2025 13:00:28

481 Views

Non-duplicate characters in a string are those that appear only once and do not repeat within the string, and the strings are a data type that represents a sequence of characters or text. Sometimes in JavaScript, we need to work with strings and manipulate them based on certain conditions. One of the common tasks is to remove characters that only appear once in a string so that we keep only the characters that appear more than once. In this article, we will understand how to remove non-duplicate characters from a string in JavaScript. Understanding the Problem Suppose we have ... Read More

Advertisements