Found 9321 Articles for Object Oriented Programming

Finding perfect numbers in JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:02:18

2K+ Views

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly.For example −28 is a perfect number, because 28 = 1 + 2 + 4 + 7 + 14We are required to write a JavaScript function that takes in a number, say n, and determines whether or not n is a perfect number.Exampleconst num = 28; const checkPerfectNumber = (num = 1) => {    if(num === 1) {       return false;    }; ... Read More

Shifting certain elements to the end of array JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:58:26

300 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument.Our function should check for all the instances of second number in the array, if there exists any, the function should push all those instances to the end of the array.If the input array is −const arr = [1, 5, 6, 6, 5, 3, 3];And the second argument is 6Then the array should become −const output = [1, 5, 5, 3, 3, 6, 6];Exampleconst arr = [1, 5, 6, 6, 5, 3, 3]; const ... Read More

JavaScript Reverse the order of the bits in a given integer

AmitDiwan
Updated on 21-Nov-2020 05:57:04

221 Views

We are required to write a JavaScript program that reverses the order of the bits in a given integer.For example −56 -> 111000 after reverse 7 -> 111Another example,234 -> 11101010 after reverse 87 -> 1010111Exampleconst num1 = 789; const num = 43 const reverseBits = (num = 1) => {    const str = num.toString(2);    const arr = str.split('').reverse();    const arrStr = arr.join('');    const reversedNum = parseInt(arrStr, 2);    return reversedNum; } console.log(reverseBits(num)); console.log(reverseBits(num1));OutputAnd the output in the console will be −53 675

JavaScript Finding the third maximum number in an array

AmitDiwan
Updated on 21-Nov-2020 05:55:54

701 Views

We are required to write a JavaScript function that takes in an array of Numbers. The function should pick and return the third highest number from the array.The time complexity of our function must not exceed O(n), we have to find the number in single iteration.Exampleconst arr = [1, 5, 23, 3, 676, 4, 35, 4, 2];  const findThirdMax = (arr) => {    let [first, second, third] = [-Infinity, -Infinity, -Infinity];    for (let el of arr) {       if (el === first || el === second || el === third) {          continue; ... Read More

Find the Length of the Longest possible palindrome string JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:54:14

326 Views

Given a string s which consists of lowercase or uppercase letters, we are required to return the length of the longest palindrome that can be built with those letters. Letters are case sensitive, for example, "Aa" is not considered a palindrome here.For example −If the input string is −const str = "abccccdd";then the output should be 7, because, one longest palindrome that can be built is "dccaccd", whose length is 7.Exampleconst str = "abccccdd"; const longestPalindrome = (str) => {    const set = new Set();    let count = 0;    for (const char of str) {     ... Read More

Finding square root of a non-negative number without using Math.sqrt() JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:52:47

274 Views

We are required to write a JavaScript function that takes in a non-negative Integer and computes and returns its square root. We can floor off a floating-point number to an integer.For example: For the number 15, we need not to return the precise value, we can just return the nearest smaller integer value that will be 3, in case of 15We will make use of the binary search algorithm to converse to the square root of the given number.The code for this will be −Exampleconst squareRoot = (num = 1) => {    let l = 0; let r = ... Read More

Reversing vowels in a string JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:51:39

297 Views

We are required to write a JavaScript function that takes a string as input and reverse only the vowels of a string.For example −If the input string is −const str = 'Hello';Then the output should be −const output = 'Holle';The code for this will be −const str = 'Hello'; const reverseVowels = (str = '') => {    const vowels = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);    let left = 0, right = str.length-1;    let foundLeft = false, foundRight = false;    str = str.split(""); while(left < right){       if(vowels.has(str[left])){   ... Read More

JavaScript group array - find the sets of numbers that can be traveled to using the edges defined

AmitDiwan
Updated on 21-Nov-2020 05:49:44

63 Views

Consider the following input and output arrays −const input = ["0:3", "1:3", "4:5", "5:6", "6:8"]; const output = [    [0, 1, 3],    [4, 5, 6, 8] ];Considering each number as a node in a graph, and each pairing x:y as an edge between nodes x and y, we are required to find the sets of numbers that can be traveled to using the edges defined.That is, in graph theory terms, find the distinct connected components within such a graph. For instance, in the above arrays, there is no way to travel from 4 to 0 so they are ... Read More

JavaScript function that takes a multidimensional and a single array, and finds matches of the single array in the multi-d array

AmitDiwan
Updated on 21-Nov-2020 05:47:48

77 Views

We are required to write a JavaScript function that takes in an array of arrays of Numbers as the first argument and an array of Numbers as the second argument. The function should pick a subarray from each array of the first array, (subarray that contains item common to both the second array and the corresponding array of first array.)For example −If the inputs are −Exampleconst arr1 = [ [1, 2, 5, 6], [5, 13, 7, 8], [9, 11, 13, 15], [13, 14, 15, 16], [1, 9, 11, 12] ]; const arr2 = [9, 11, 13, 15, 1, 2, 5, ... Read More

How to find and return the longest repeating series of numbers in array with JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:45:29

514 Views

We are required to write a JavaScript function that takes in an array of Numbers that may contain some repeating elements. The function should return the length of the longest repeating number sequence from the array.For example −If the input array is −const arr = [2, 1, 1, 2, 3, 3, 2, 2, 2, 1];Then the output should be 3 because the number 2 is repeated 3 times consecutively in the array (and that's the highest number).Exampleconst arr = [2, 1, 1, 2, 3, 3, 2, 2, 2, 1]; const findLongestSequence = (arr = []) => {    const res ... Read More

Advertisements