Found 9316 Articles for Object Oriented Programming

String function to replace nth occurrence of a character in a string JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:47:30

882 Views

We are required to write a function removeStr() that lives on String.prototype object and takes in a string str, a character char and a number n.The function should remove the nth appearance of char from str.Let’s write the code for this −const str = 'aaaaaa'; const subStr = 'a'; const num = 6; removeStr = function(subStr, num){    if(!this.includes(subStr)){       return -1;    }    let start = 0, end = subStr.length;    let occurences = 0;    for(; ;end < this.length){       if(this.substring(start, end) === subStr){          occurences++;       }; ... Read More

Pair of (adjacent) elements of an array whose sum is lowest JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:41:17

325 Views

We are required to write a JavaScript function that takes in an array of numbers. The function should return a subarray of two adjacent elements from the original array whose sum is the least amongst all adjacent pairs of the array.If the length of the array is less than 2, we should return boolean false.For example, If the input array is −const arr = [41, 44, -12, 13, -23, 1, 5, -4, 2, 2];Here, the sum of pair [-23, 1] is -22 which is the least for any two adjacent elements of the array, so the function should return [-23, ... Read More

Subarrays product sum in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:38:03

125 Views

We are required to write a JavaScript function that takes in an array of numbers of length N such that N is a positive even integer and divides the array into two sub arrays (say, left and right) containing N/2 elements each.The function should do the product of the subarrays and then add both the results thus obtained.For example, If the input array is −const arr = [1, 2, 3, 4, 5, 6]Then the output should be −(1*2*3) + (4*5*6) 6+120 126The code for this will be −const arr = [1, 2, 3, 4, 5, 6] const subArrayProduct = arr ... Read More

Sum of all the non-repeating elements of an array JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:35:21

351 Views

Suppose, we have an array of numbers like this −const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14];We are required to write a JavaScript function that takes in one such array and counts the sum of all the elements of the array that appear only once in the array −For example:The output for the array mentioned above will be −356The code for this will be −const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14]; const nonRepeatingSum = arr => {    let res = 0;    for(let i = 0; i < arr.length; i++){       if(i !== arr.lastIndexOf(arr[i])){          continue;       };       res += arr[i];    };    return res; }; console.log(nonRepeatingSum(arr));Following is the output on console −30

Checking for Fibonacci numbers in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:31:52

1K+ Views

We are required to write a JavaScript function that takes in a number and checks whether it is a Fibonacci number or not (i.e., it falls in Fibonacci series or not).Our function should return true if the number is a Fibonacci number, false otherwise.The code for this will be −const num = 2584; const isFibonacci = num => {    if(num === 0 || num === 1){       return true;    }    let prev = 1;    let count = 2;    let temp = 0;    while(count

Finding first unique element in sorted array in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:30:04

117 Views

Suppose, we have a sorted array of literals like this −const arr = [32, 32, 63, 63, 63, 75, 75, 86, 87, 88, 89];We are required to write a JavaScript function that takes in one such array and returns the first unique number in the array.If there is no such number in the array, our function should return false.For this array, the output should be 86.The code for this will be −const arr = [32, 32, 63, 63, 63, 75, 75, 86, 87, 88, 89]; const firstUnique = arr => {    let appeared = false;    for(let i = ... Read More

Finding smallest number using recursion in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:28:15

141 Views

We are required to write a JavaScript function that takes in an array of Numbers and returns the smallest number from it using recursion.Let’s say the following are our arrays −const arr1 = [-2, -3, -4, -5, -6, -7, -8]; const arr2 = [-2, 5, 3, 0];The code for this will be −const arr1 = [-2, -3, -4, -5, -6, -7, -8]; const arr2 = [-2, 5, 3, 0]; const min = arr => {    const helper = (a, ...res) => {       if (!res.length){          return a;       };     ... Read More

JavaScript Get English count number

AmitDiwan
Updated on 09-Oct-2020 11:26:21

111 Views

We are required to write a JavaScript function that takes in a number and returns an English count number for it.For example 3 returns 3rdThe code for this will be −const num = 3; const englishCount = num => {    if (num % 10 === 1 && num % 100 !== 11){       return num + "st";    };    if (num % 10 === 2 && num % 100 !== 12) {       return num + "nd";    };    if (num % 10 === 3 && num % 100 !== 13) {       return num + "rd";    };    return num + "th"; }; console.log(englishCount(num)); console.log(englishCount(111)); console.log(englishCount(65)); console.log(englishCount(767));Following is the output on console −3rd 111th 65th 767th

How to find capitalized words and add a character before that in a given sentence using JavaScript?

AmitDiwan
Updated on 09-Oct-2020 11:24:21

144 Views

Suppose, we have a string that contains some capitalized English alphabets like this −const str = "Connecting to server Connection has been successful We found result";We are required to write a JavaScript function that takes in one such string and inserts a comma ', ' before the space before every capital letter in the string.The code for this will be −const str = "Connecting to server Connection has been successful We found result"; const capitaliseNew = str => {    let newStr = '';    const regex = new RegExp(/.[A-Z]/g);    newStr = str.replace(regex, ', $&');    return newStr; }; ... Read More

Remove smallest number in Array JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:22:01

584 Views

We are required to write a JavaScript function that takes in an array of numbers. The number should find the smallest element in the array and remove it in place.The code for this will be −const arr = [2, 1, 3, 2, 4, 5, 1]; const removeSmallest = arr => {    const smallestCreds = arr.reduce((acc, val, index) => {       let { num, ind } = acc;       if(val >= num){          return acc;       };       ind = index;       num = val;       return { ind, num };    }, {       num: Infinity,       ind: -1    });    const { ind } = smallestCreds;    if(ind === -1){       return;    };    arr.splice(ind, 1); }; removeSmallest(arr); console.log(arr);Following is the output on console −[ 2, 3, 2, 4, 5, 1 ]

Advertisements