Found 6686 Articles for Javascript

Natural Sort in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:57:34

660 Views

We have an array that contains some numbers and some strings, we are required to sort the array such that the numbers get sorted and get placed before every string and then the string should be placed sorted alphabetically.For exampleLet’s say this is our array −const arr = [1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9];The output should look like this −[1, 6, 7, 9, 47, 'afv', 'bdf', 'fdf', 'svd']Therefore, let’s write the code for this −const arr = [1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9]; const sorter = (a, b) => {    if(typeof a === ... Read More

JavaScript Strings: Replacing i with 1 and o with 0

AmitDiwan
Updated on 09-Oct-2020 11:55:05

117 Views

We are required to write a function that takes in a string as one and only argument and returns another string that has all ‘i’ and ‘o’ replaced with ‘1’ and ‘0’ respectively.It’s one of those classic for loop problems where we iterate over the string with its index and construct a new string as we move through.The code for the function will be −const string = 'Hello, is it raining in Amsterdam?'; const validate = (str) => {    let validatedString = '';    for(let i = 0; i < str.length; i++){       if(str[i] === 'a'){   ... Read More

Checking for the Gapful numbers in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:53:24

62 Views

A number is a gapful number when −It has at least three digits, andIt is exactly divisible by the number formed by putting its first and last digits togetherFor example:1053 is a gapful number because it has 4 digits and it is exactly divisible by 13. 135 is a gapful number because it has 3 digits and it is exactly divisible by 15.Our job is to write a program that returns the nearest gapful number to the number we provide as input.Let’s write the code −const n = 134; //receives a number string and returns a boolean const isGapful = ... Read More

Removing a specific substring from a string in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:51:00

333 Views

We are given a main string and a substring, our job is to create a function, let’s say removeString() that takes in these two arguments and returns a version of the main string which is free of the substring.Here, we need to remove the separator from a string, for example −this-is-a-stingLet’s now write the code for this function −const removeString = (string, separator) => {    //we split the string and make it free of separator    const separatedArray = string.split(separator);    //we join the separatedArray with empty string    const separatedString = separatedArray.join("");    return separatedString; } const str ... Read More

String function to replace nth occurrence of a character in a string JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:47:30

886 Views

We are required to write a function removeStr() that lives on String.prototype object and takes in a string str, a character char and a number n.The function should remove the nth appearance of char from str.Let’s write the code for this −const str = 'aaaaaa'; const subStr = 'a'; const num = 6; removeStr = function(subStr, num){    if(!this.includes(subStr)){       return -1;    }    let start = 0, end = subStr.length;    let occurences = 0;    for(; ;end < this.length){       if(this.substring(start, end) === subStr){          occurences++;       }; ... Read More

Pair of (adjacent) elements of an array whose sum is lowest JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:41:17

331 Views

We are required to write a JavaScript function that takes in an array of numbers. The function should return a subarray of two adjacent elements from the original array whose sum is the least amongst all adjacent pairs of the array.If the length of the array is less than 2, we should return boolean false.For example, If the input array is −const arr = [41, 44, -12, 13, -23, 1, 5, -4, 2, 2];Here, the sum of pair [-23, 1] is -22 which is the least for any two adjacent elements of the array, so the function should return [-23, ... Read More

Subarrays product sum in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:38:03

126 Views

We are required to write a JavaScript function that takes in an array of numbers of length N such that N is a positive even integer and divides the array into two sub arrays (say, left and right) containing N/2 elements each.The function should do the product of the subarrays and then add both the results thus obtained.For example, If the input array is −const arr = [1, 2, 3, 4, 5, 6]Then the output should be −(1*2*3) + (4*5*6) 6+120 126The code for this will be −const arr = [1, 2, 3, 4, 5, 6] const subArrayProduct = arr ... Read More

Sum of all the non-repeating elements of an array JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:35:21

355 Views

Suppose, we have an array of numbers like this −const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14];We are required to write a JavaScript function that takes in one such array and counts the sum of all the elements of the array that appear only once in the array −For example:The output for the array mentioned above will be −356The code for this will be −const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14]; const nonRepeatingSum = arr => {    let res = 0;    for(let i = 0; i < arr.length; i++){       if(i !== arr.lastIndexOf(arr[i])){          continue;       };       res += arr[i];    };    return res; }; console.log(nonRepeatingSum(arr));Following is the output on console −30

Checking for Fibonacci numbers in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:31:52

1K+ Views

We are required to write a JavaScript function that takes in a number and checks whether it is a Fibonacci number or not (i.e., it falls in Fibonacci series or not).Our function should return true if the number is a Fibonacci number, false otherwise.The code for this will be −const num = 2584; const isFibonacci = num => {    if(num === 0 || num === 1){       return true;    }    let prev = 1;    let count = 2;    let temp = 0;    while(count

Finding first unique element in sorted array in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:30:04

117 Views

Suppose, we have a sorted array of literals like this −const arr = [32, 32, 63, 63, 63, 75, 75, 86, 87, 88, 89];We are required to write a JavaScript function that takes in one such array and returns the first unique number in the array.If there is no such number in the array, our function should return false.For this array, the output should be 86.The code for this will be −const arr = [32, 32, 63, 63, 63, 75, 75, 86, 87, 88, 89]; const firstUnique = arr => {    let appeared = false;    for(let i = ... Read More

Advertisements