Found 6683 Articles for Javascript

Euclidean Algorithm for calculating GCD in JavaScript

AmitDiwan
Updated on 11-Dec-2020 08:46:15

1K+ Views

In mathematics, Euclid's algorithm, is a method for computing the greatest common divisor (GCD) of two numbers, the largest number that divides both of them without leaving a remainder.The Euclidean algorithm is based on the principle that the greatest common divisor of two numbers does not change if the larger number is replaced by its difference with the smaller number.For example, 21 is the GCD of 252 and 105 (as 252 = 21 × 12 and 105 = 21 × 5), and the same number 21 is also the GCD of 105 and 252 − 105 = 147.Since this replacement ... Read More

Primality test of numbers in JavaScript

AmitDiwan
Updated on 11-Dec-2020 08:44:50

195 Views

A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. All other natural numbers greater than 1 are called composite numbers. A primality test is an algorithm for determining whether an input number is prime.We are required to write a JavaScript function that takes in a number and checks whether it is a prime or not.ExampleFollowing is the code −const findPrime = (num = 2) => {    if (num % 1 !== 0) {       return false;    }    if (num

Comparing the performance of recursive and looped factorial function in JavaScript

AmitDiwan
Updated on 10-Dec-2020 08:30:06

129 Views

We will be writing two JavaScript functions, the job of both the functions will be to take in a number and return its factorial.The first function should make use of a for loop or while loop to compute the factorial. Whereas the second function should compute the factorial using a recursive approach.Lastly, we should compare the times taken by these functions over a large number of iterations.ExampleFollowing is the code −const factorial = (num = 1) => {    let result = 1;    for (let i = 2; i {    if(num > 1){       return ... Read More

Insert a character at nth position in string in JavaScript

AmitDiwan
Updated on 10-Dec-2020 08:28:29

449 Views

We are required to write a JavaScript function that takes in a string as the first argument and a number as the second argument and a single character as the third argument, let’s call this argument char.The number is guaranteed to be smaller than the length of the array. The function should insert the character char after every n characters in the string and return the newly formed string.For example −If the arguments are −const str = 'NewDelhi'; const n = 3; const char = ' ';Then the output string should be −const output = 'Ne wDe lhi';ExampleFollowing is the ... Read More

Trim off '?' from strings in JavaScript

AmitDiwan
Updated on 10-Dec-2020 08:27:01

65 Views

We are required to write a JavaScript function that takes in a string as the only argument. The string is likely to contain question marks (?) in the beginning and the end. The function should trim off all these question marks from the beginning and the end keeping everything else in place.For example −If the input string is −const str = '??this is a ? string?';Then the output should be −const output = 'this is a ? string';ExampleFollowing is the code −const str = '??this is a ? string?'; const specialTrim = (str = '', char = '?') => { ... Read More

Converting array of arrays into an object in JavaScript

AmitDiwan
Updated on 10-Dec-2020 08:25:39

1K+ Views

Suppose we have an array of arrays that contains the performance of a cricket player like this −const arr = [    ['Name', 'V Kohli'],    ['Matches', 13],    ['Runs', 590],    ['Highest', 183],    ['NO', 3],    ['SR', 131.5] ];We are required to write a JavaScript function that takes in one such array of arrays. Here, each subarray represents one key-value pair, the first element being the key and the second its value. The function should construct an object based on the key-value pairs in the array and return the object.Therefore, for the above array, the output should look ... Read More

Converting object to 2-D array in JavaScript

AmitDiwan
Updated on 10-Dec-2020 08:24:31

643 Views

Suppose we have an object that contains information about the weather of a city −const obj = {    city: "New Delhi",    maxTemp: 32,    minTemp: 21,    humidity: 78,    aqi: 456,    day: 'Tuesday', };We are required to write a JavaScript function that takes in one such object. The function should construct an array of arrays based on this object where each subarray contains exactly two properties −the corresponding keythat key's valueTherefore, for the above object, the output should look like −const output = [    [ 'city', 'New Delhi' ],    [ 'maxTemp', 32 ],   ... Read More

Check for specific beginning or ending letters in a JavaScript function

AmitDiwan
Updated on 10-Dec-2020 08:22:09

54 Views

We are required to write a JavaScript function that takes in two strings. Let’s call them str1 and str2.Our function should check either str1 starts with str2 or it ends with str2. If this is the case, we should return true otherwise we should return false.ExampleFollowing is the code −const str = 'this is an example string'; const startsOrEndsWith = (str1 = '', str2 = '') => {    if(str2.length > str1.length){       return false;    };    if(str1 === str2){       return true;    };    const { length: l1 } = str1;    const ... Read More

Finding sum of alternative elements of the array in JavaScript

AmitDiwan
Updated on 10-Dec-2020 08:20:58

818 Views

We are required to write a JavaScript function that takes in an array of Numbers as the only argument. The function should calculate and return the sum of alternative elements of the array.For example −If the input array is −const arr = [1, 2, 3, 4, 5, 6, 7];Then the output should be −1 + 3 + 5 + 7 = 16ExampleFollowing is the code −const arr = [1, 2, 3, 4, 5, 6, 7]; const alternativeSum = (arr = []) => {    let sum = 0;    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(i % 2 !== 0){          continue;       };       sum += el;    };    return sum; }; console.log(alternativeSum(arr));OutputFollowing is the output on console −16

Awkward behaviour of delete operator on arrays in JavaScript

AmitDiwan
Updated on 10-Dec-2020 08:19:37

75 Views

The delete operator in JavaScript is actually an object operator (used with objects).But since arrays are also indexed objects in JavaScript, we can use the delete operator with arrays as well.Consider the following array of literals −const arr = ['a', 'b', 'c', 'd', 'e'];ExampleLet us now execute the following program and guess the expected output −const arr = ['a', 'b', 'c', 'd', 'e']; delete arr[4]; console.log(arr); console.log(arr.length);OutputThe output of this program in the console will be −[ 'a', 'b', 'c', 'd', ] 5Understanding the output −Since we deleted one index value of the array, we expected the array.length to ... Read More

Advertisements