Found 6710 Articles for Javascript

Adding binary strings together JavaScript

Nikitasha Shrivastava
Updated on 18-Aug-2023 18:12:15

2K+ Views

The problem is stating that we have to add two binary strings. Binary strings are a sequence of bytes. To add them in javascript, we will first convert them to decimal and calculate the sum. After adding those decimal numbers, we will convert it again into binary strings and print the output. What are Binary Strings? Binary numbers or binary strings are sequences of bytes in base 2. It is basically a combination of 0 and 1 to represent any number. While binary addition is one of the operations of mathematics to perform on binary strings. Binary addition ... Read More

Merging sorted arrays together JavaScript

Nikitasha Shrivastava
Updated on 18-Aug-2023 19:22:23

846 Views

Merging the sorted arrays together in javascript can be done using the sorting techniques available in data structures. In this problem we will be given two sorted arrays and we have to merge them. After merging them, return a single array which is also in a sorted form. For solving this problem we will use greedy approach. What are Sorted Arrays ? In javascript arrays are the collection of similar data types and we can resize the array in run time. Sorted arrays are sequential forms of items of an array. Their order should be ... Read More

Validating a power JavaScript

Nikitasha Shrivastava
Updated on 18-Aug-2023 19:34:49

174 Views

In this problem we have to validate that a given number is a power of n. If the given number is power of n then it should return the true value of it otherwise it should return false. To check this kind of validation we generally use arithmetic operators like Modulus - %, Division - /, comparison operators like less than < or greater than >. Lets understand this problem with an example. True value: N = 27 So 33 = 27 False Value: N = 28 So 33 != ... Read More

Determining isomorphic strings JavaScript

AmitDiwan
Updated on 23-Nov-2020 07:08:50

787 Views

Two strings (str1 and str2) are isomorphic if the characters in str1 can be replaced to get str2.For example −const str1 = 'abcde'; const str2 = 'eabdc';These two are an example of isomorphic stringsWe are required to write a JavaScript function that in two strings. The function should determine whether or not the two input strings are isomorphic.Exampleconst str1 = 'abcde'; const str2 = 'eabdc'; const isIsomorphic = (str1 = '', str2 = '') => {    if (str1.length !== str2.length) {       return false;    };    for (let i = 0;    i < str1.length; i++) ... Read More

Counting prime numbers from 2 upto the number n JavaScript

AmitDiwan
Updated on 23-Nov-2020 07:07:59

414 Views

We are required to write a JavaScript function that takes in a number, say n, as the first and the only argument.The function should then return the count of all the prime numbers from 2 upto the number n.For example −For n = 10, the output should be: 4 (2, 3, 5, 7) For n = 1, the output should be: 0Exampleconst countPrimesUpto = (num = 1) => {    if (num < 3) {       return 0;    };    let arr = new Array(num).fill(1);    for (let i = 2; i * i < num; i++) ... Read More

Finding trailing zeros of a factorial JavaScript

AmitDiwan
Updated on 23-Nov-2020 07:06:56

422 Views

Given an integer n, we have to write a function that returns the number of trailing zeroes in n!.For example −trailingZeroes(4) = 0 trailingZeroes(5) = 1 because 5! = 120 trailingZeroes(6) = 1Exampleconst num = 17; const findTrailingZeroes = num => {    let cur = 5, total = 0;    while (cur

Calculate average from JSON data based on multiple filters JavaScript

AmitDiwan
Updated on 23-Nov-2020 07:05:38

1K+ Views

Suppose, we have an array of objects like this −const arr = [    { "SupplierName" : "John", "Category " : "A", "Points" : 3 },    { "SupplierName" : "John", "Category " : "A", "Points" : 11 },    { "SupplierName" : "John", "Category " : "A", "Points" : undefined },    { "SupplierName" : "John", "Category " : "B", "Points" : 2 },    { "SupplierName" : "John", "Category " : "B", "Points" : 6 },    { "SupplierName" : "Praveen", "Category " : "A", "Points" : 3 },    { "SupplierName" : "Praveen", "Category " : "A", ... Read More

Sort array based on min and max date in JavaScript?

AmitDiwan
Updated on 23-Nov-2020 07:03:07

606 Views

Suppose, we have an array of string dates like this −const arr = [    "2017-01-22 00:21:17.0",    "2017-01-27 11:30:23.0",    "2017-01-24 15:53:21.0",    "2017-01-27 11:34:18.0",    "2017-01-26 16:55:48.0",    "2017-01-22 11:57:12.0",    "2017-01-27 11:35:43.0" ];We are required to write a JavaScript function that takes in one such array. The function should find the oldest and the newest date from this array.And then the function should finally return an object containing those two dates.Exampleconst arr = [    "2017-01-22 00:21:17.0",    "2017-01-27 11:30:23.0",    "2017-01-24 15:53:21.0",    "2017-01-27 11:34:18.0",    "2017-01-26 16:55:48.0",    "2017-01-22 11:57:12.0",    "2017-01-27 11:35:43.0" ]; const ... Read More

Group a JavaScript Array

AmitDiwan
Updated on 23-Nov-2020 07:01:10

315 Views

Suppose, we have a JavaScript array like this −const data = [    {       "dataId": "1",       "tableName": "table1",       "column": "firstHeader",       "rows": [          "a", "b", "c"       ]    },    {       "dataId": "2",       "tableName": "table1",       "column": "secondHeader",       "rows": [          "d", "e", "f",       ]    }, {       "dataId": "3",       "tableName": "table2",       "column": "aNewFirstHeader",     ... Read More

How to transform two or more spaces in a string in only one space? JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:58:05

301 Views

We have to write a JavaScript program that takes a variable user string through an input in HTML. Then through JavaScript the program should check for more than one consecutive spaces in the string.And the program should replace all such instances of more than one consecutive spaces with only one space.We can use a regular expression as the first parameter of replace. /\s{2, }/g to achieve the desired results. Let us write the code for this function −Example Live Demo REMOVE SPACES    function removeSpaces() {       var textInput = insertText.value;   ... Read More

Advertisements