Found 6710 Articles for Javascript

How to take screenshot of a div with JavaScript

AmitDiwan
Updated on 22-Feb-2021 14:01:44

2K+ Views

We are required to capture (convert into image) part(s) of our markup that lays out our website and save that captured image or do something with it. So, we are required to devise a way using which we can achieve this described behaviour.As our problem includes capturing any markup element and not just canvas, it’s a bit complex and especially if we plan to do it from scratch. Therefore, for our ease we will use a third party library, htmltocanvas that does exactly what the name suggests, converting the desired markup to canvas, after which we can simply download the canvas ... Read More

How to read JSON file in JavaScript?

AmitDiwan
Updated on 24-Sep-2024 17:19:28

5K+ Views

There are three effective methods to read JSON files in JavaScript using fetch(), import statements, and require(). Please check this article which includes examples and outputs with the approach explanation. JSON (JavaScript Object Notation) is a lightweight data interchange format that's easy for humans to read and write and easy for machines to parse and generate. In this article, we'll explore three different approaches to reading JSON files in JavaScript: Approaches to Read JSON file Using the fetch() method Using the import statement Using the require ... Read More

Write a program in JavaScript to check if two strings are anagrams of each other or not

Dev Prakash Sharma
Updated on 05-Feb-2021 11:57:40

22K+ Views

Given two strings ‘a’ and string ‘b’, we have to check if they are anagrams of each other or not and return True/False. For example, Input-1 −String a= “india” String b= “nidia”Output −TrueExplanation − Since the given string ‘b’ contains all the characters in the string ‘a’ thus we will return True.Input-2 −String a= “hackathon” String b= “achcthoon”Output −FalseExplanation − Since the given string ‘b’ doesn’t have all the characters as string ‘a’ have thus we will return False.The approach used to solve this problemIn the given strings ‘a’ and ‘b’, we will check if they are of the same ... Read More

Counting How Many Numbers Are Smaller Than the Current Number in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:35:15

266 Views

We are required to write a JavaScript function that takes in an array of Numbers.The function should construct a new array based on the input array.Each corresponding element of the new array should be the number of elements that are smaller in the original array than that corresponding element.For example −If the input array is −const arr = [2, 7, 3, 1, 56, 4, 7, 8];Then the output array should be −const output = [1, 4, 2, 0, 7, 3, 4, 6 ];ExampleFollowing is the code −const arr = [2, 7, 3, 1, 56, 4, 7, 8]; const smallerThanCurrent = ... Read More

Finding Lucky Numbers in a Matrix in JavaScript

Nikitasha Shrivastava
Updated on 18-May-2023 14:54:05

865 Views

In the given problem statement we have to write a function to get the lucky numbers in a matrix with the help of Javascript. So we will implement this task with for loops and functions of Javascript. Understanding the problem statement The problem statement is asking us to find the lucky numbers in a given matrix. So the lucky number will be defined as a number in the matrix which is the minimum value for its own row and the maximum value for its column. For example we have a matrix [ [3, 7], [9, 11] ], in this ... Read More

Finding the group with largest elements with same digit sum in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:31:10

138 Views

We are required to write a JavaScript function that takes in a positive integer, say n, as the only argument.The function should first group the integers from 1 to n to subarray where a specific subarray contains all the elements contains a specific digit sum. Then the function should examine each subarray and return the length of that subarray which contains the most elements.For example −If the input number is −const num = 15;Then the output should be −const output = 2;because the groups are −[1, 10], [2, 11], [3, 12], [4, 13], [5, 14], [6, 15], [7], [8], [9]ExampleFollowing ... Read More

Grouping words with their anagrams in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:29:21

1K+ Views

Anagrams:Two words or phrases which can be made by arranging the letters of each other in a different order are called anagrams of each other, like rat and tar.We are required to write a JavaScript function that takes in an array of strings that might contain some anagram strings. The function should group all the anagrams into separate subarrays and return the new array thus formed.For example −If the input array is −const arr = ['rat', 'jar', 'tar', 'raj', 'ram', 'arm', 'mar', 'art'];Then the output array should be −const output = [    ['rat', 'tar', 'art'],    ['jar', 'raj'],   ... Read More

Finding hamming distance in a string in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:27:24

590 Views

Hamming Distance:The hamming distance between two strings of equal length is the number of positions at which these strings vary.In other words, it is a measure of the minimum number of changes required to turn one string into another. Hamming Distance is usually measured for strings equal in length.We are required to write a JavaScript function that takes in two strings, lets say str1 and str2, of the same length. The function should calculate and return the hamming distance between those strings.ExampleFollowing is the code −const str1 = 'Hello World'; const str2 = 'Heeyy World'; const findHammingDistance = (str1 = ... Read More

Counting the number of 1s upto n in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:25:30

269 Views

We are required to write a JavaScript function that takes in a positive integer, say num.The task of our function is to count the total number of 1s that appears in all the positive integers upto n (including n, if it contains any 1).Then the function should finally return this count.For example −If the input number is −const num = 31;Then the output should be −const output = 14;because 1 appears in, 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 31ExampleFollowing is the code −const num = 31; const countOnes = (num = 1) => { ... Read More

Finding the Largest Triple Product Array in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:23:31

178 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument.Based on the array taken in as input, the function should construct a new array of the same length based on the following criteria.Any corresponding element of the output array should be the product of the three largest numbers encountered thus far. If the corresponding index is less than 3 (we have not encountered three elements yet) then the corresponding value should be -1. And Although we can use non-unique values to calculate the product, but those non-unique values should be present ... Read More

Advertisements