- 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 9326 Articles for Object Oriented Programming
72 Views
We have two sets of ranges; one is a single range of any length (R1) and the other is a set of ranges (R2) some or parts of which may or may not lie within the single range (R1).We need to calculate the sum of the ranges in (R2) - whole or partial - that lie within the single range (R1).const R1 = [20, 40]; const R2 = [[14, 22], [24, 27], [31, 35], [38, 56]];Result = 2+3+4+2 = 11R1 = [120, 356]; R2 = [[234, 567]];Result 122ExampleLet us write the code −const R1 = [20, 40]; const R2 = [[14, 22], ... Read More
2K+ Views
We are required to write a JavaScript function that takes in an array of literals and a number and splits the array (first argument) into groups each of length n (second argument) and returns the two-dimensional array thus formed.If the array and number is −const arr = ['a', 'b', 'c', 'd']; const n = 2;Then the output should be −const output = [['a', 'b'], ['c', 'd']];ExampleLet us now write the code −const arr = ['a', 'b', 'c', 'd']; const n = 2; const chunk = (arr, size) => { const res = []; for(let i = 0; i ... Read More
456 Views
For this, use Object.keys() along with reduce(). To display the result, we will also use concat().ExampleFollowing is the code −var details = { name: ["John", "David"], age1: "21", age2: "23" }, output = Object .keys(details) .reduce((obj, tempKey) => (obj[tempKey] = [].concat(details[tempKey]), obj), {}) console.log(output) To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo302.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo302.js { name: [ 'John', 'David' ], age1: [ '21' ], age2: [ '23' ] }
77 Views
Let’s say the following is our array −var values = [7,5,3,8,9,'/',9,5,8,2,'/',3,4,8];To merge specific elements, use map along with split().ExampleFollowing is the code −var values = [7,5,3,8,9,'/',9,5,8,2,'/',3,4,8]; var afterMerge = values.join('') .split(/(\d+)/). filter(Boolean). map(v => isNaN(v) ? v : +v); console.log(afterMerge);To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo301.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo301.js [ 75389, '/', 9582, '/', 348 ]
262 Views
For this, you can use ternary operator based on some condition.ExampleFollowing is the code −const yearRangeValue = 18; const getCorrectCentury = dateValues => { var [date, month, year] = dateValues.split("-"); var originalYear = +year > yearRangeValue ? "20" + year : "18" + year; return new Date(date + "-" + month + "-" + originalYear).toLocaleDateString('en-GB') }; console.log(getCorrectCentury('10-JAN-19'));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo300.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo300.js 1/10/2019
327 Views
The get keyword can be used as a getter function like C#, Java and other technologies.We set a function with get like the following in a class −class Employee { constructor(name) { this.name = name; } get fullName() { return this.name; } }ExampleFollowing is the code displaying an example of get −class Employee { constructor(name) { this.name = name; } get fullName() { return this.name; } } var employeeObject = new Employee("David Miller"); console.log(employeeObject.fullName);To run the above program, you need to use ... Read More
291 Views
We are required to write a JavaScript function that takes in two 2-D arrays of numbers and returns their matrix multiplication result.Let’s say the following are our two matrices −// 5 x 4 let a = [ [1, 2, 3, 1], [4, 5, 6, 1], [7, 8, 9, 1], [1, 1, 1, 1], [5, 7, 2, 6] ]; // 4 x 6 let b = [ [1, 4, 7, 3, 4, 6], [2, 5, 8, 7, 3, 2], [3, 6, 9, 6, 7, 8], [1, 1, 1, 2, 3, 6] ];ExampleLet’s ... Read More
102 Views
We are required to write a JavaScript function that takes in a string and any number of characters specified as separators. Our function should return a splitted array of the string based on all the separators specified.For example −If the string is −const str = 'rttt.trt/trfd/trtr, tr';And the separators are −const sep = ['/', '.', ', '];Then the output should be −const output = [ 'rttt', 'trt', 'trfd', 'trtr' ];ExampleFollowing is the code −const str = 'rttt.trt/trfd/trtr, tr'; const splitMultiple = (str, ...separator) => { const res = []; let start = 0; for(let i = 0; ... Read More
264 Views
Given an array of integers, we are required to write a function that takes this array and finds the one element that appears an odd number of times. There will always be only one integer that appears an odd number of times.We will approach this problem by sorting the array. Once sorted, we can iterate over the array to pick the element that appears for odd number of times.ExampleFollowing is the code −const arr = [20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]; const findOdd = arr => { let ... Read More
243 Views
Let’s say the following is our string. Some text is surrounded by special character hash(#) −var values = "My Name is #yourName# and I got #marks# in JavaScript subject";We need to replace the special character with valid values. For this, use replace() along with shift().ExampleFollowing is the code −var values = "My Name is #yourName# and I got #marks# in JavaScript subject"; const originalValue = ["David Miller", 97]; var result = values.replace(/#([^#]+)#/g, _ => originalValue.shift()); console.log(result);To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo298.js.OutputThis will produce the following output ... Read More