We are required to write a JavaScript function that takes in a string that may contain some backward slashes.And the function should return a new string where all the backslashes with forward slashes.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'Th/s str/ng /conta/ns some/ forward slas/hes'; const invertSlashes = str => { let res = ''; for(let i = 0; i < str.length; i++){ if(str[i] !== '/'){ res += str[i]; continue; }; ... Read More
We are required to write a JavaScript function that takes in a string and checks whether it is sorted or not.For example:isSorted('adefgjmxz') // true isSorted('zxmfdba') // true isSorted('dsfdsfva') // falseTherefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'abdfhlmxz'; const findDiff = (a, b) => a.charCodeAt(0) - b.charCodeAt(0); const isStringSorted = (str = '') => { if(str.length < 2){ return true; }; let res = '' for(let i = 0; i < str.length-1; i++){ if(findDiff(str[i+1], str[i]) > 0){ ... Read More
Suppose we have a list of numbers called nums, we have to check whether every number can be grouped using one of the following rules: 1. Contiguous pairs (a, a) 2. Contiguous triplets (a, a, a) 3. Contiguous triplets (a, a + 1, a + 2)So, if the input is like nums = [7, 7, 3, 4, 5], then the output will be True, as We can group [7, 7] together and [3, 4, 5] together.To solve this, we will follow these steps −n := size of numsdp := a list of size n+1, first value is True, others are ... Read More
Suppose we have an array of objects like this −const array = [ {key: 'a', value: false}, {key: 'a', value: 100}, {key: 'a', value: null}, {key: 'a', value: 23} ];We are required to write a JavaScript function that takes in one such array and places all the objects that have falsy values for the "value" property to the bottom and sorts all other objects in decreasing order by the "value" property.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [ {key: 'a', value: false}, {key: 'a', ... Read More
Suppose we have a value n. We have to find all upside down numbers of length n. As we knot the upside down number is one that appears the same when flipped 180 degrees.So, if the input is like n = 2, then the output will be ['11', '69', '88', '96'].To solve this, we will follow these steps −Define a function middle() . This will take xif x is 0, thenreturn list of a blank stringif x is same as 1, thenreturn a new list of elements 0, 1, 8ret := a new listmid := middle(x − 2)for each m ... Read More
We are required to write a JavaScript function that takes in a string which contains English alphabets. The function should return an object containing the count of vowels and consonants in the string.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'This is a sample string, will be used to collect some data'; const countAlpha = str => { return str.split('').reduce((acc, val) => { const legend = 'aeiou'; let { vowels, consonants } = acc; if(val.toLowerCase() === val.toUpperCase()){ ... Read More
Suppose we have a binary tree, we have to check whether all nodes in the tree have the same values or not.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps −Define a function solve() . This will take root, and valif root is null, thenreturn Trueif val is not defined, thenval := value of rootreturn true when value of root is same as val and solve(left of root, val) and solve(right of root, val) are also trueLet us see the following implementation to get better understanding −Example Live Democlass TreeNode: def ... Read More
Suppose we have a list of numbers nums (positive or negative), we have to check whether the number of occurrences of every value in the array is unique or not.So, if the input is like nums = [6, 4, 2, 9, 4, 2, 2, 9, 9, 9], then the output will be True, as there is 1 occurrence of 6, 2 occurrences of 4, 3 occurrences of 2, and 4 occurrences of 9. So all number of occurrences are unique.To solve this, we will follow these steps −num_counts := a new map where all values and number of occurrences of ... Read More
We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return an array of all the elements from the input array that are greater than or equal to the number taken as the second argument.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [56, 34, 2, 7, 76, 4, 45, 3, 3, 34, 23, 2, 56, 5]; const threshold = 40; const findGreater = (arr, num) => { const res = ... Read More
Suppose we have a matrix called requested_trips where each row containing [start_x, end_x, num_passengers], and we also have a capacity value. Now each requested trip asks to pick up num_passengers passenger at start_x and drop them off at end_x. We also have a car with the capacity that is given, and start at position x = 0. We want to to pick up every passenger and can only move right side, we have to check whether we can pick up and drop off everyone.So, if the input is like trips = [[1, 25, 2], [3, 4, 3], [5, 12, 3]] ... Read More