Found 10483 Articles for Web Development

Function that parses number embedded in strings - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:47:07

125 Views

Conventionally, we have functions like parseInt() and parseFloat() that takes in a string and converts the number string to Number. But these methods fail when we have numbers embedded at random index inside the string.For example: The following would only return 454, but what we want is 4545453 −parseInt('454ffdg54hg53')So, we are required to write a JavaScript function that takes in such string and returns the corresponding number.ExampleFollowing is the code −const numStr = '454ffdg54hg53'; const parseInteger = numStr => {    let res = 0;    for(let i = 0; i < numStr.length; i++){       if(!+numStr[i]){     ... Read More

Reverse sum of two arrays in JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:46:03

454 Views

We are required to write a JavaScript function that takes in two arrays of numbers of the same length. The function should return an array with any arbitrary nth element of the array being the sum of nth term from start of first array and nth term from last of second array.For example −If the two arrays are −const arr1 = [34, 5, 3, 3, 1, 6]; const arr2 = [5, 67, 8, 2, 6, 4];Then the output should be −const output = [38, 11, 5, 11, 68, 11];ExampleFollowing is the code −const arr1 = [34, 5, 3, 3, 1, ... Read More

How to access variables declared in a function, from another function using JavaScript?

AmitDiwan
Updated on 18-Sep-2020 09:44:57

2K+ Views

We have to write a function, that does some simple task, say adding two numbers or something like that. We are required to demonstrate the way we can access the variables declared inside that function in some other function or globally.ExampleFollowing is the code −const num = 5; const addRandomToNumber = function(num){    // a random number between [0, 10)    const random = Math.floor(Math.random() * 10);    // assigning the random to this object of function    // so that we can access it outside    this.random = random;    this.res = num + random; }; const addRandomInstance = ... Read More

If ([] == false) is true, why does ([] || true) result in []? - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:43:55

526 Views

If we look closely at the problem statement, the difference between ([] == false) and ([] || true) is the following −In the first case, we are using loose conditional checking, allowing type coercion to take over.While in the second case, we are evaluating [] to its respective Boolean (truthy or falsy) which makes use of the function Boolean() instead of type coercion under the hook.Let's now unveil the conversions that happens behind the scenes in both cases.Case 1 − ([] == false)According to the MDN docs if two data types say x and y are compared using the loose ... Read More

Sorting or Arranging an Array with standard array values - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:42:44

179 Views

We are required to sort a dynamic JavaScript array. The condition is that we are required to sort it according to the values stored in a particular order in a standard predefined array.Let’s say the following is our dynamic array −const dbArray = ['Apple', 'Banana', 'Mango', 'Apple', 'Mango', 'Mango', 'Apple'];And suppose the standard array against which we have to sort the above array is like −const stdArray = ['Mango', 'Apple', 'Banana', 'Grapes'];So, after sorting the dbArray, my resultant array should look like −const resultArray = ['Mango', 'Mango', 'Mango', 'Apple', 'Apple', 'Apple', 'Banana'];ExampleFollowing is the code −const dbArray = ['Apple', 'Banana', ... Read More

Finding letter distance in strings - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:41:32

705 Views

We are required to write a JavaScript function that takes in a string as first argument and two single element strings. The function should return the distance between those single letter stings in the string taken as first argument.For example −If the three strings are −const str = 'Disaster management'; const a = 'i', b = 't';Then the output should be 4 because the distance between 'i' and 't' is 4ExampleFollowing is the code −const str = 'Disaster management'; const a = 'i', b = 't'; const distanceBetween = (str, a, b) => {    const aIndex = str.indexOf(a);   ... Read More

Finding shared element between two strings - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:39:08

585 Views

We are required to write a JavaScript function that takes in two strings that may / may not contain some common elements. The function should return an empty string if no common element exists otherwise a string containing all common elements between two strings.Following are our two strings −const str1 = 'Hey There!!, how are you'; const str2 = 'Can this be a special string';ExampleFollowing is the code −const str1 = 'Hey There!!, how are you'; const str2 = 'Can this be a special string'; const commonString = (str1, str2) => {    let res = '';    for(let i ... Read More

Adding a function for swapping cases to the prototype object of strings - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:38:08

138 Views

In JavaScript, we can write our own custom functions and assign them to the existing standard data types (it is quite similar to writing library methods but in this case the data types are primitive and not user defined. We are required to write a JavaScript String function by the name, let’s say swapCase().This function will return a new string with all uppercase characters swapped for lower case characters, and vice versa. Any non-alphabetic characters should be kept as they are.ExampleFollowing is the code −const str = 'ThIS iS A CraZY StRInG'; String.prototype.swapCase = function(){    let res = ''; ... Read More

Mapping the letter of a string to an object of arrays - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:36:56

450 Views

Given a string, we are required to write a function that creates an object that stores the indexes of each letter in an array. The letters (elements) of the string must be the keys of objectThe indexes should be stored in an array and those arrays are values.For example −If the input string is −const str = 'cannot';Then the output should be −const output = {    'c': [0],    'a': [1],    'n': [2, 3],    'o': [4],    't': [5] };ExampleFollowing is the code −const str = 'cannot'; const mapString = str => {    const map = ... Read More

Determining full house in poker - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:35:54

432 Views

   The “full house in poker” is a situation when a player, out of their five cards, has at least three cards identical. We are required to write a JavaScript function that takes in an array of five elements representing a card each and returns true if there's a full house situation, false otherwise.ExampleFollowing is the code −const arr2 = ['K', '2', 'K', 'A', 'J']; const isFullHouse = arr => {    const copy = arr.slice();    for(let i = 0; i < arr.length; ){       const el = copy.splice(i, 1)[0];       if(copy.includes(el)){       ... Read More

Advertisements