Found 6683 Articles for Javascript

Split array entries to form object in JavaScript

Nikitasha Shrivastava
Updated on 18-Aug-2023 19:30:35

261 Views

The problem statement is saying that we have to find a solution for a given array and split it to form an object with a key-value pair in javascript. Understand the problem In simple terms we have to convert a given array into an object. There are several methods present in javascript to convert an array into an object. We will use and code each of them one by one. Using reduce() and split() methods In this method we will use reduce and split functions of javascript. Algorithm The algorithm here discusses ... Read More

JavaScript Group a JSON Object by Two Properties and Count

Nikitasha Shrivastava
Updated on 27-Jun-2024 17:43:01

2K+ Views

The problem stated that we have to add two properties of the JSON object and count its occurrences. For example we have name and age properties in JSON then group them in a single property and count their occurrences. What is JSON? JSON (Javascript Object Notation) is lightweight data to transfer between devices. It is human readable and writable data. JSON is in the form of key-value pairs. Keys are strings to define values. In JSON each entry is separated by a semicolon. For example - {“name” : “Peter”}, in this example name is a key and Peter ... Read More

How to get single array from multiple arrays in JavaScript

Nikitasha Shrivastava
Updated on 18-Aug-2023 18:56:17

3K+ Views

In this problem we will understand how to get a single array from multiple arrays and different ways to solve this problem. We will use three methods to solve this problem: the first method is with the help of the spread operator, second concat method and finally last method using push with the spread operator. Understanding the problem We have given two or more arrays and we have to concatenate them in a single array. There can be many arrays given and after concatenating them it should appear as a single array. Let’s understand with ... Read More

Get average of every group of n elements in an array JavaScript

Nikitasha Shrivastava
Updated on 18-Aug-2023 18:28:38

783 Views

In the given problem, we have to find out the average of every group of n elements in the array. If there are m elements in the array, then calculate the average of every group and show the result in array form. Understanding the Problem Suppose we have 6 elements in the array and make a group of 2 elements. Now calculate the average of every group individually. Now that we have their average values, we can combine them into a single array. To implement this problem we have used reduce(), push(), and slice(). These ... Read More

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

341 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

97 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

691 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

372 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

362 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

Advertisements