- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9318 Articles for Object Oriented Programming
768 Views
We are required to write a JavaScript function that takes in a nested array of Numbers and returns the sum of all the numbers present in the array.Let’s say the following is our nested array −const arr = [2, 5, 7, [ 4, 5, 4, 7, [ 5, 7, 5 ], 5 ], 2];ExampleFollowing is the code −const arr = [2, 5, 7, [ 4, 5, 4, 7, [ 5, 7, 5 ], 5 ], 2]; const calculateSum = (arr, query) => { let count = 0; for(let i = 0; i < arr.length; i++){ if(Array.isArray(arr[i])){ count += calculateSum(arr[i], query); continue; }; count += arr[i]; }; return count; }; console.log(calculateSum(arr));OutputThis will produce the following output in console −58
84 Views
We are required to write a JavaScript function that takes in a number, sums its digits and checks whether that sum is a Palindrome number or not. The function should return true if the sum is Palindrome, false otherwise.For example, if the number is 697, Then the sum of its digit will be 22, which indeed, is a Palindrome number. Therefore, our function should return true for 697.ExampleFollowing is the code −const num = 697; const sumDigit = (num, sum = 0) => { if(num){ return sumDigit(Math.floor(num / 10), sum + (num % 10)); }; ... Read More
172 Views
We are required to write a JavaScript function that accepts an array of Numbers and a number, say n (n must be less than or equal to the length of array). And our function should replace the kth element from the beginning with the nth element from the end of the array.ExampleFollowing is the code −const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const swapNth = (arr, k) => { const { length: l } = arr; let temp; const ind = k-1; temp = arr[ind]; arr[ind] = arr[l-k]; arr[l-k] = temp; }; swapKth(arr, 4); console.log(arr); swapNth(arr, 8); console.log(arr);OutputThis will produce the following output in console −[ 0, 1, 2, 6, 4, 5, 3, 7, 8, 9 ] [ 0, 1, 7, 6, 4, 5, 3, 2, 8, 9 ]
301 Views
Given a string S, consisting of alphabets, numbers and special characters. We need to write a program to split the strings in three different strings S1, S2 and S3, such that −The string S1 will contain all the alphabets present in S, The string S2 will contain all the numbers present in S, andS3 will contain all special characters present in S.The strings S1, S2 and S3 should have characters in the same order as they appear in input.ExampleFollowing is the code −const str = "Th!s String C0nt@1ns d1fferent ch@ract5rs"; const seperateCharacters = str => { const strArr = ... Read More
4K+ Views
Suppose, we have an object like this −const dataset = { "diamonds":77, "gold-bars":28, "exciting-stuff":52, "oil":51, "sports-cars":7, "bitcoins":40 };We are required to write a JavaScript function that takes one such object and returns an array of objects that have keys and their values splitted.Therefore, for the above object, the output should be −const output = [ {"asset":"diamonds", "quantity":77}, {"asset":"gold-bars", "quantity":28}, {"asset":"exciting-stuff", "quantity":52}, {"asset":"oil", "quantity":51}, {"asset":"bitcoins", "quantity":40} ];ExampleFollowing is the code −const dataset = { "diamonds":77, "gold-bars":28, "exciting-stuff":52, "oil":51, "sports-cars":7, "bitcoins":40 }; const splitKeyValue = ... Read More
657 Views
We are required to write a JavaScript function that takes in a string and returns the character from the string that appears for second most number of times.Let’s say the following is our string −const str = 'This string will be used to calculate frequency';Above, the second most frequent character is “e”.ExampleLet us now see the complete code −const str = 'This string will be used to calculate frequency'; const secondMostFrequent = str => { const strArr = str.split(''); const map = strArr.reduce((acc, val) => { if(acc.has(val)){ acc.set(val, acc.get(val) + 1); ... Read More
99 Views
We are required to write a JavaScript function that takes in an array of strings and deletes each one of the two strings that ends with the same character −For example, If the actual array is −const arr = ['Radar', 'Cat' , 'Dog', 'Car', 'Hat'];Then we have to delete one and keep only one string ending with the same character in the array of distinct letters.ExampleFollowing is the code −const arr = ['Radar', 'Cat' , 'Dog', 'Car', 'Hat']; const delelteSameLetterWord = arr => { const map = new Map(); for(let i = 0; i < arr.length; ){ ... Read More
105 Views
Suppose we have an array of objects like this −const arr = [ {val: 56, canUse: true}, {val: 16, canUse: true}, {val: 45, canUse: true}, {val: 76, canUse: false}, {val: 45, canUse: true}, {val: 23, canUse: false}, {val: 23, canUse: false}, {val: 87, canUse: true}, ];We are required to write a JavaScript function that calculates the average of the val property of all those objects that have a boolean true set for the canUse flag.ExampleFollowing is the code −const arr = [ {val: 56, canUse: true}, {val: 16, canUse: true}, ... Read More
259 Views
We are required to write a JavaScript function that takes in three numbers. Let's say the three numbers are a, b and n.Our job is to find all the n-digit numbers whose sum of digits at even positions and odd positions are divisible by a and b respectively. And we have to lastly return an array containing all the required numbers, the array should be empty if there are no matching numbers.ExampleFollowing is the code −const indexSum = (num, sumOdd = 0, sumEven = 0, index = 0) => { if(num){ if(index % 2 === 0){ ... Read More
121 Views
We are required to write a JavaScript function that takes in an array and a number n.Our function should rotate the array by n elements, i.e., take n elements from the front and put them to the end.The only condition here is that we have to do this without using any extra space in memory −For example −If the input array is the following, const arr = [12, 6, 43, 5, 7, 2, 5];and number n is 3, then the output should be;const output = [5, 7, 2, 5, 12, 6, 43];ExampleFollowing is the code −const arr = [12, 6, ... Read More