
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 9150 Articles for Object Oriented Programming

913 Views
We are required to write a JavaScript function that takes in a string that contains some one-digit numbers in between and the function should return the sum of all the numbers present in the string.Let’s say the following is our string with numbers −const str = 'gdf5jhhj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3';ExampleLet's write the code for this −const str = 'gdf5jhhj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3'; const sumStringNum = str => { const strArr = str.split(""); let res = 0; for(let i = 0; i < strArr.length; i++){ if(+strArr[i]){ res += +strArr[i]; }; }; return ... Read More

239 Views
We are required to write a JavaScript function that takes in an array of numbers and returns the alternative multiplicative sum of the elementsFor example −If the array is −const arr = [1, 2, 4, 1, 2, 3, 4, 3];then the output should be calculated like this −1*2+4*1+2*3+4*3 2+4+6+12And the output should be −24ExampleLet's write the code for this −const arr = [1, 2, 4, 1, 2, 3, 4, 3]; const alternateOperation = arr => { const productArr = arr.reduce((acc, val, ind) => { if(ind % 2 === 1){ return acc; }; acc.push(val * (arr[ind + 1] || 1)); return acc; }, []); return productArr.reduce((acc, val) => acc + val); }; console.log(alternateOperation(arr));OutputThe output in the console: −24

1K+ Views
Suppose we have an object like this −const obj = { name: "Vikas", age: 45, occupation: "Frontend Developer", address: "Tilak Nagar, New Delhi", experience: 23, };We are required to write a JavaScript function that takes in such an object with key value pairs and converts it into a Map.ExampleLet's write the code for this −const obj = { name: "Vikas", age: 45, occupation: "Frontend Developer", address: "Tilak Nagar, New Delhi", experience: 23, salary: "98000" }; const objectToMap = obj => { const keys = Object.keys(obj); const map ... Read More

516 Views
We are required to write a JavaScript function that takes in a nested array of literals and converts it to a string by concatenating all the values present in it to the stringconst arr = [ 'hello', [ 'world', 'how', [ 'are', 'you', [ 'without', 'me' ] ] ] ];ExampleLet’s say the following is our nested array −const arr = [ 'hello', [ 'world', 'how', [ 'are', 'you', [ 'without', 'me' ] ] ] ]; const arrayToString = (arr) => { let str = ''; for(let i = 0; i < arr.length; i++){ if(Array.isArray(arr[i])){ str += arrayToString(arr[i]); }else{ str += arr[i]; }; }; return str; }; console.log(arrayToString(arr));OutputFollowing is the output in the console −helloworldhowareyouwithoutme

1K+ Views
ASCII CodeASCII is a 7-bit character code where every single bit represents a unique character.Every English alphabet has a unique decimal ascii code.We are required to write a function that takes in two strings and calculates their ascii scores (i.e., the sum of ascii decimal of each character of string) and returns the difference.ExampleLet's write the code for this −const str1 = 'This is the first string.'; const str2 = 'This here is the second string.'; const calculateScore = (str = '') => { return str.split("").reduce((acc, val) => { return acc + val.charCodeAt(0); }, 0); ... Read More

449 Views
We are required to compare the time taken respectively by the ES6 functions forEach() and reduce() for summing a huge array of numbers.As we can't have a huge array of numbers here, we will simulate the magnitude of array by performing the summing operation for large number of times (iterations)ExampleLet's write the code for this −const arr = [1, 4, 4, 54, 56, 54, 2, 23, 6, 54, 65, 65]; const reduceSum = arr => arr.reduce((acc, val) => acc + val); const forEachSum = arr => { let sum = 0; arr.forEach(el => sum += el); return ... Read More

1K+ Views
We are required to write a JavaScript function that takes in two numbers, say, a and b and returns the total number of prime numbers between a and b (including a and b, if they are prime).For example −If a = 2, and b = 21, the prime numbers between them are 2, 3, 5, 7, 11, 13, 17, 19And their count is 8. Our function should return 8.Let’s write the code for this function −ExampleFollowing is the code −const isPrime = num => { let count = 2; while(count < (num / 2)+1){ if(num ... Read More

767 Views
Suppose, we have a currency system where we have denominations of 1000 units, 500 units, 100 units, 50 units, 20 units, 10 units, 5 units, 2 units and 1 unit.Given a specific amount, we are required to write a function that calculates the least number of total denominations that sum up to the amount.For example, if the amount is 512, The least number of notes that will add up to it will be: 1 unit of 500, 1 unit of 10 and 1 unit of 2.So, in this we for 512, our function should return 3, i.e., the total count ... Read More

400 Views
In this article, we will learn to calculate the union of two objects in JavaScript. The union of two objects results in a new object that contains all the properties of both objects. What is the Union of Two Objects? The union of two objects is the process of combining the properties of both objects into a single object. For example Input − const obj1 = { name: " ", email: " " }; const obj2 = { name: ['x'], email: ['y']}; Output − const output = { name: {" ", [x]}, email: {" ", [y]} }; Different Approaches ... Read More

193 Views
We are required to write a JavaScript function that takes in two strings and concatenates the second string to the first string.If the last character of the first string and the first character of the second string are the same then we have to omit one of those characters. Let’s say the following are our strings in JavaScript −const str1 = 'Food'; const str2 = 'dog';Let’s write the code for this function −const str1 = 'Food'; const str2 = 'dog'; const concatenateStrings = (str1, str2) => { const { length: l1 } = str1; const { length: l2 ... Read More