Check if String1 Ends with String2 in JavaScript

AmitDiwan
Updated on 31-Aug-2020 07:00:38

119 Views

We are required to write a JavaScript function that takes in two strings, say, string1 and string2 and determines whether the string1 ends with string2 or not.For example −"The game is on" Here, "on" should return trueWhile, "the game is off" Above, “of" should return falseLet's write the code for this function −Exampleconst first = 'The game is on'; const second = ' on'; const endsWith = (first, second) => {    const { length } = second;    const { length: l } = first;    const sub = first.substr(l - length, length);    return sub === second; }; ... Read More

Check for Ugly Number in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:59:27

1K+ Views

In the decimal number system, ugly numbers are those positive integers whose only prime factors are 2, 3 or 5.For example − Integers from 1 to 10 are all ugly numbers, 12 is an ugly number as well.Our job is to write a JavaScript function that takes in a Number and determines whether it is an ugly number or not.Let's write the code for this function −Exampleconst num = 274; const isUgly = num => {    while(num !== 1){       if(num % 2 === 0){          num /= 2;       } else ... Read More

Reverse All the Words of a Sentence in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:56:12

4K+ Views

We are required to write a JavaScript function that takes in a string and returns a new string which has all the words reversed from the original string.For example −If the original string is −"Hello World how is it outside"Then the output should be −"olleH dlroW woH si ti edistuo"Now, let's write the code for this function −Exampleconst str = 'Hello World how is it outside'; const reverseSentence = str => {    const arr = str.split(" ");    const reversed = arr.map(el => {       return el.split('').reverse().join("");    });    return reversed.join(" "); }; console.log(reverseSentence(str));OutputThe output in ... Read More

Remove All Characters of First String from Second in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:53:32

371 Views

Let’s say, we have two strings that contains characters in no specific order. We are required to write a function that takes in these two strings and returns a modified version of the second string in which all the characters that were present in the first string are omitted.Following are our strings −const first = "hello world"; const second = "hey there";Following is our function to remove all characters of first string from second −const removeAll = (first, second) => {    const newArr = second.split("").filter(el => {       return !first.includes(el);    });    return newArr.join(""); };Let's write ... Read More

Smallest Positive Value That Cannot Be Represented as Sum of Subarray in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:52:36

143 Views

We have a sorted array of positive integers like this −const arr = [1, 3, 6, 10, 11, 15];We are required to write a function, say findSmallest() that takes in one such array and returns the smallest positive integer that cannot be represented as the sum of some subarray of this original array.For example −For this array written above 2 is the smallest positive integer which cannot be reached by summing any subarray of this original array. So, now let's write the code for this function. As the array is sorted, we can achieve the solution to this problem in ... Read More

Return Top Two Elements from Array in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:51:08

325 Views

We have an array of numbers in JavaScript that contains numbers in an unsorted order. Our job is to write a function that takes in this array of numbers and returns an array of two elements, the top two elements of the array (greatest two elements of the array).We have to do this in one pass i.e., we need to execute this method in linear time like by using only one for loop or if we use ES6 function, we have to make sure to use only one and once and avoid nesting of methods which increases time complexity.So, now ... Read More

Finding First Non-Repeating Character in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:49:38

766 Views

We have an array of Numbers/String literals where most of the entries are repeated. Our job is to write a function that takes in this array and returns the index of first such element which does not make consecutive appearances.If there are no such elements in the array, our function should return -1. So now, let's write the code for this function. We will use a simple loop to iterate over the array and return where we find non-repeating characters, if we find no such characters, we return -1 −Exampleconst arr = ['d', 'd', 'e', 'e', 'e', 'k', 'j', 'j', ... Read More

Find Difference Between Largest and Smallest Number in Array in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:45:54

351 Views

We have an array of Numbers that are arranged in pure random order. Our job is to write a function that takes in one such array of Numbers and returns the difference of greatest and smallest numbers present in it, but without sorting the array.Therefore, let's write the code for this function −We will use the Array.prototype.reduce() function to pick the smallest and greatest numbers from the array and later will return their difference. The code for this function will be −Exampleconst arr = [23, 65, 67, 23, 2, 6, 87, 23, 45, 65, 3, 234, 3]; const findDifference = ... Read More

Find All Elements That Appear More Than N Times in JavaScript Array

AmitDiwan
Updated on 31-Aug-2020 06:44:32

394 Views

We have an array of Number/String literals that contains some repeated entries. Our job is to write a function that takes in a positive integer Number n and returns a subarray of all the elements that makes appearance more than or equal to the number n specified by the only argument.Therefore, let's write the code for this function −We will use a Map() to keep count of the frequency of elements and later return the elements that exceed the specified count. The code for this will be −Exampleconst arr = [34, 6, 34, 8, 54, 7, 87, 23, 34, 6, ... Read More

Searching Objects by Value in JavaScript

AmitDiwan
Updated on 31-Aug-2020 06:43:17

153 Views

Suppose we have an object like this −const obj = {    "name": "Vivek Sharma",    "occupation": "Software Engineer",    "age": 23,    "contacts": [{       "name": "Mukul Sharma",       "occupation": "Software Engineer",       "age": 31,    }, {       "name": "Jay Sharma",       "occupation": "Software Engineer",       "age": 27,    }, {       "name": "Rajan Sharma",       "occupation": "Software Engineer",       "age": 32,    }] };Here it is nested up to only one level, but the nesting can be deeper as ... Read More

Advertisements