Found 6710 Articles for Javascript

Odd even index difference - JavaScript

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

775 Views

We are required to write a JavaScript function that takes in an array of numbers like this −const arr = [3, 6, 34, 12, 6, 8, 8, 5, 6, 8];The function should return the difference between the sum of elements present at the odd index and the sum of elements present at even indexExampleFollowing is the code −const arr = [3, 6, 34, 12, 6, 8, 8, 5, 6, 8]; const oddEvenDiff = arr => {    let diff = 0;    for(let i = 0; i < arr.length; i++){       if(i % 2 === 0){          diff += arr[i];       }else{          diff -= arr[i]       };    };    return Math.abs(diff); }; console.log(oddEvenDiff(arr));OutputThis will produce the following output in console −18

Matching strings for similar characters - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:53:31

574 Views

We are required to write a JavaScript function that accepts two string and a number n.The function matches the two strings i.e., it checks if the two strings contains the same characters.The function returns true if both the strings contain the same character irrespective of their order or if they contain at most n different characters, else the function should return false.ExampleFollowing is the code −const str = 'some random text'; const str2 = 'some r@ndom text'; const deviationMatching = (first, second, num) => {    let count = 0;    for(let i = 0; i < first.length; i++){   ... Read More

Segregating a string into substrings - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:52:22

216 Views

We are required to write a JavaScript function that takes in a string and a number n as two arguments (the number should be such that it exactly divides the length of string) and we have to return an array of n strings of equal length.For example −If the string is "how" and the number is 2, our output should be −["h", "o", "w"];Here, every substring exactly contains −(length of array/n) charactersAnd every substring is formed by taking corresponding first and last letters of the string alternatively.ExampleFollowing is the code −const str = "how"; const num = 3; const segregate ... Read More

Sorting an array of binary values - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:50:37

492 Views

Let’s say, we have an array of Numbers that contains only 0, 1 and we are required to write a JavaScript function that takes in this array and brings all 1s to the start and 0s to the end.For example − If the input array is −const arr = [1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1];Then the output should be −const output = [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0];ExampleFollowing is the code −const arr = [1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1]; const sortBinary = arr => {    const copy = [];    for(let i = 0; i − arr.length; i++){       if(arr[i] === 0){          copy.push(0);       }else{          copy.unshift(1);       };       continue;    };    return copy; }; console.log(sortBinary(arr));OutputFollowing is the output in the console −[    1, 1, 1, 1, 1,    1, 0, 0, 0, 0,    0 ]

Arranging words in Ascending order in a string - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:49:28

389 Views

Let’s say, we are required to write a JavaScript function that takes in a string and returns a new string with words rearranged according to their increasing length.ExampleFollowing is the code −const str = 'This is a sample string only'; const arrangeByLength = str => {    const strArr = str.split(' ');    const sorted = strArr.sort((a, b) => {       return a.length - b.length;    });    return sorted.join(' '); }; console.log(arrangeByLength(str));OutputFollowing is the output in the console −a is This only sample string

Finding the ASCII score of a string - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:48:28

455 Views

ASCII CodeASCII is a 7-bit character code where every single bit represents a unique character.Every English alphabet has a unique decimal ascii code.We are required to write a JavaScript function that takes in a string and counts the sum of all the ascii codes of the string charactersExampleFollowing is the code −const str = 'This string will be used for calculating ascii score'; const calculateASCII = str => {    let res = 0;    for(let i = 0; i < str.length; i++){       const num = str[i].charCodeAt(0);       res += num;    };    return res; }; console.log(calculateASCII(str));OutputFollowing is the output in the console −4946

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

453 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

525 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

Advertisements