Found 9321 Articles for Object Oriented Programming

Checking the validity of parentheses in JavaScript

AmitDiwan
Updated on 24-Nov-2020 06:47:13

2K+ Views

We are required to write a JavaScript function that takes in a string str containing just the characters −'(', ')', '{', '}', '[' and ']'Our function should determine if the input string is valid.An input string is valid if −Open brackets must be closed by the same type of brackets.Open brackets must be closed in the correct order.For example −"()" is a valid parenthesis"()[]{}" is a valid parentheses"(]" is an invalid parenthesisExampleThe code for this will be −const str = "()[]{}"; const isValid = (str = '') => {    const map=new Map();    map.set('{', '}');    map.set('(', ')');   ... Read More

Finding the longest "uncommon" sequence in JavaScript

AmitDiwan
Updated on 24-Nov-2020 06:45:16

107 Views

We are required to write a JavaScript function that takes in an array of strings. The function should find the longest uncommon subsequence among the strings of the array.By longest uncommon subsequence we mean the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.Our function should return the length of this longest uncommon subsequence.For example: If the input array is −const arr = ["aba", "cdc", "eae"];Then the output should be 3.ExampleThe code for this will be −const arr = ["aba", "cdc", "eae"]; const findUncommonLength = (array = []) => { ... Read More

Difference between first and the second array in JavaScript

AmitDiwan
Updated on 23-Nov-2020 11:29:27

134 Views

We are required to write a JavaScript function that takes in two arrays of literals. The arrays might contain some identical entries as well.The purpose of our function is to simply find out and return an array of all such elements that exists in the first array but not in the second.ExampleThe code for this will be −const arr1 = ['1', '2', '3', '4/2', '5/4', '6−2']; const arr2 = ['1', '2', '3', '5/4', '4/2', '6−1', '7/2', '8−2']; const differenceBetween = (arr1 = [], arr2 = []) => {    const res = [];    for(let i = 0; i < ... Read More

Find the closest index to given value in JavaScript

AmitDiwan
Updated on 23-Nov-2020 11:28:24

331 Views

We are required to write a JavaScript function that takes in an array of numbers as the first input and a single number as the second input.The function should find and return the index of the number from the array which is closest to the number specified by second argument.ExampleThe code for this will be −const arr = [0, 65, 131, 196, 259, 323, 388, 453, 517]; const target = 425; const findClosest = (arr, target) => {    let min;    let chosen = 0;    for (let i in arr) {       min = Math.abs(arr[chosen] − ... Read More

Using a recursive function to capitalize each word in an array in JavaScript

AmitDiwan
Updated on 23-Nov-2020 11:27:17

327 Views

We are required to write a JavaScript function that takes in an array of String literals. The function should do the following two things −Make use of recursive approachMake first word of each string element capital.Our function should do this without using extra space for storing another array.For example −If the input array is −const arr = ['apple', 'banana', 'orange', 'grapes'];Then the array should be transformed to −const output = ['Apple', 'Banana', 'Orange', 'Grapes'];ExampleThe code for this will be −const arr = ['apple', 'banana', 'orange', 'grapes']; const capitalize = (arr = [], ind = 0) => {    const helper ... Read More

How to store two arrays as a keyvalue pair in one object in JavaScript?

AmitDiwan
Updated on 23-Nov-2020 11:26:07

663 Views

Suppose, we have two arrays of literals of same length like these −const arr1 = ['firstName', 'lastName', 'age', 'address', 'isEmployed']; const arr2 = ['Rahul', 'Sharma', 23, 'Tilak Nagar', false];We are required to write a JavaScript function that takes in two such arrays.The function should construct an object mapping the elements of the second array to the corresponding elements of the first array.We will use the Array.prototype.reduce() method to iterate over the arrays, building the object.ExampleThe code for this will be −const arr1 = ['firstName', 'lastName', 'age', 'address', 'isEmployed']; const arr2 = ['Rahul', 'Sharma', 23, 'Tilak Nagar', false]; const mapArrays = ... Read More

Program to make vowels in string uppercase and change letters to next letter in alphabet (i.e. z->a) in JavaScript

AmitDiwan
Updated on 23-Nov-2020 11:24:56

445 Views

We are required to write a JavaScript function that takes in a string as the only input.The function should construct a new string based on the input string in which all the vowels should be uppercased and change each alphabet to the corresponding next alphabet.For example − If the input string is −const str = 'newString';Therefore, the output for the above input should look like this −const output = 'oExSusIoh';ExampleThe code for this will be −const str = 'newString'; const capitiliseAndMove = (str = '') => {    let s = '';    s = str.replace(/[a−z]/g, function(c) {     ... Read More

Reversing the alphabet from back to front and front to back in JavaScript

AmitDiwan
Updated on 23-Nov-2020 11:23:26

204 Views

We are required to write a JavaScript function that takes in a single alphabet as the only input. The function should compute the position of the that alphabet from starting and return an alphabet that’s at the same position but from the back.ExampleThe code for this will be −const alpha = 'g'; const findCounterPart = (alpha = '') => {    let alphabet = 'abcdefghijklmnopqrstuvwxyz';    let firstpart = alphabet.substring(0, 13).split('');    let secondpart = alphabet.substring(13).split('').reverse();    let solution = '';    if (firstpart.indexOf(alpha) !== −1) {       solution = secondpart[firstpart.indexOf(alpha)];    } else {       ... Read More

Possible combinations and convert into alphabet algorithm in JavaScript

AmitDiwan
Updated on 23-Nov-2020 11:21:52

165 Views

Suppose we are given the mapping a = 1, b = 2, ... z = 26, and an encoded message. We are required to write a JavaScript function that takes in the message.The function should count the number of ways it can be decoded.For example, the message '111' would give 3, since it could be decoded as 'aaa, 'ka', and 'ak'.ExampleThe code for this will be −const waysToProcess = ( message, ways = 0 ) => {    if ( message.length ) {       ways = waysToProcess( message.slice( 1 ,message.length), ways );       const numCurr = ... Read More

Expressive words problem case in JavaScript

AmitDiwan
Updated on 23-Nov-2020 11:20:37

146 Views

Sometimes people repeat letters to represent extra feeling, such as "hello" −> "heeellooo", "hi" −> "hiiii". In these strings like "heeellooo", we have groups of adjacent letters that are all the same: "h", "eee", "ll", "ooo".For some given string S, a query word is stretchy if it can be made to be equal to S by any number of applications of the following extension operation: choose a group consisting of characters c, and add some number of characters c to the group so that the size of the group is 3 or more.For example, starting with "hello", we could do ... Read More

Advertisements