Found 9326 Articles for Object Oriented Programming

Compare two arrays of single characters and return the difference? JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:48:17

394 Views

We are required to compare, and get the difference, between two arrays containing single character strings appearing multiple times in each array.Example of two such arrays are −const arr1 = ['A', 'C', 'A', 'D']; const arr2 = ['F', 'A', 'T', 'T'];We will check each character at the same position and return only the parts who are different.Exampleconst arr1 = ['A', 'C', 'A', 'D']; const arr2 = ['F', 'A', 'T', 'T']; const findDifference = (arr1, arr2) => {    const min = Math.min(arr1.length, arr2.length);    let i = 0;    const res = [];    while (i < min) {   ... Read More

Finding the index position of an array inside an array JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:46:21

190 Views

Suppose, we have an array of arrays like this −const arr = [    [1, 0],    [0, 1],    [0, 0] ];We are required to write a JavaScript function that takes in one such array as the first argument and an array of exactly two Numbers as the second argument.Our function should check whether or not the array given by second input exists in the original array of arrays or not.Exampleconst arr = [ [1, 0], [0, 1], [0, 0] ];  const sub = [0, 0]; const matchEvery = (arr, ind, sub) => arr[ind].every((el, i) => el == sub[i]); ... Read More

Get range of months from array based on another array JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:44:50

320 Views

Suppose we have two arrays of strings. The first array contains exactly 12 strings, one for each month of the year like this −const year = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];The second array, contains exactly two strings, denoting a range of months like this −const monthsRange = ["aug", "oct"];We are required to write a JavaScript function that takes in two such arrays. Then the function should pick all the months from the first array that falls in the range specified by the second range arrays.Like for the above arrays, the output should be ... Read More

Sorting an array of objects by an array JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:43:10

519 Views

Suppose, we have an array of objects and an array of strings like this −Exampleconst orders = [    { status: "pending"},    { status: "received" },    { status: "sent" },    { status: "pending" } ]; const statuses = ["pending", "sent", "received"];We are required to write a JavaScript function that takes in two such arrays. The purpose of the function should be to sort the orders array according to the elements of the statuses array.Therefore, objects in the first array should be arranged according to the strings in the second array.Exampleconst orders = [    { status: "pending" ... Read More

Checking for overlapping times JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:41:44

2K+ Views

We are required to write a JavaScript function that takes in an array of intervals (start and end time like this −const arr = [    { start: '01:00', end: '04:00' },    { start: '05:00', end: '08:00' },    { start: '07:00', end: '11:00' },    { start: '09:30', end: '18:00' }, ];Our function should iterate through this array of objects and check all elements of the array against the others.If an overlapping interval is found, the iteration stops and true is returned, Otherwise false. By overlapping intervals, we mean time intervals that have some time in common.Exampleconst arr ... Read More

Finding the number of words in a string JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:40:16

300 Views

We are required to write a JavaScript function that takes in a string of any length. The function should then count the number of words in that string.Exampleconst str = 'THis is an example string'; const findWords = (str = '') => {    if(!str.length){       return 0;    };    let count = 1;    for(let i = 0; i < str.length; i++){       if(str[i] === ' '){          count++;       };    };    return count; }; console.log(findWords(str));OutputAnd the output in the console will be −5

Picking the triangle edges with maximum perimeter JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:39:05

155 Views

The perimeter of a triangle is the sum of all three sides of the triangle. We are required to write a JavaScript function that takes in an array of numbers of at least three or more elements.Our function should pick three longest sides (largest numbers) from the array that when summed can give the maximum perimeter from the array, we need to make sure that the three picked side can make a triangle in reality. If three exists no three sides in the array that can make a valid triangle, then we have to return zero.A valid triangle is that ... Read More

Difference between two strings JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:37:14

1K+ Views

We are given two strings, say s and t. String t is generated by random shuffling string s and then add one more letter at a random position.We are required to write a JavaScript function that takes both these strings and returns the letter that was added to t.For example −If the input stings are −const s = "abcd", t = "abcde";Then the output should be −const output = "e";because 'e' is the letter that was added.Exampleconst s = "abcd", t = "abcde"; const findTheDifference = (s, t) => {    let a = 0, b = 0; let charCode, ... Read More

Finding the nth digit of natural numbers JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:36:11

288 Views

We know that natural numbers in Mathematics are the numbers starting from 1 and spanning infinitely.First 15 natural numbers are −1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Therefore, the first natural digit is 1, second is 2, third is 3 and so on. But when we exceed 9, then tenth natural digit is the first digit of 10 i.e., 1 and 11th natural digit is the next i.e., 0.We are required to write a JavaScript function that takes in a number, say n, and finds and returns the nth natural digit.Exampleconst findNthDigit = ... Read More

Finding the longest valid parentheses JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:34:43

241 Views

Given a string containing just the characters '(' and ')', we find the length of the longest valid (well-formed) parentheses substring.A set of parentheses qualifies to be a well-formed parentheses, if and only if, for each opening parentheses, it contains a closing parentheses.For example −'(())()' is a well-formed parentheses '())' is not a well-formed parentheses '()()()' is a well-formed parenthesesExampleconst str = '(())()(((';    const longestValidParentheses = (str = '') => {       var ts = str.split('');       var stack = [], max = 0;       ts.forEach((el, ind) => {         ... Read More

Advertisements