Found 6683 Articles for Javascript

Flattening a JSON object in JavaScript

AmitDiwan
Updated on 22-Feb-2021 10:20:27

11K+ Views

Suppose, we have the following JSON object that may contain nesting upto any level −const obj = {    "one": 1,    "two": {       "three": 3    },    "four": {       "five": 5,       "six": {          "seven": 7       },       "eight": 8    },    "nine": 9 };We are required to write a JavaScript function that takes in one such nested JSON object and returns a new object that contains no nesting and maps the corresponding values to the keys using the dot ... Read More

Finding two numbers that produce equal to the sum of rest in JavaScript

AmitDiwan
Updated on 22-Feb-2021 10:22:40

244 Views

Suppose following is the problem:We have a sequence of numbers starting from 1 and upto any arbitrary number, let's call it num. We have to pick two such numbers from the sequence (let's call them m and n), such that:sum(1 to num) - (m + n) = m * nAnd finally, we should return an array of groups of all such numbers.For example −If the input is −const num = 10;Then the output should be −const output = [    [7, 6] ];because sum(1 to 10) = 55and, 55 - (6 + 7) = 6 * 7 = 42ExampleThe code ... Read More

How to set cookie value with AJAX request in JavaScript?

AmitDiwan
Updated on 22-Feb-2021 09:27:40

3K+ Views

We are required to set cookies with AJAX requests or in such a way that any AJAX request sends those cookies to the server.One thing to note here is that every AJAX request made to any remote server automatically sends all our cookies to that very server without us having to do anything. Therefore, with this thing clear, we just have to set a specific key to our document object using JavaScript and whenever we make a network call, that cookie will automatically be sent to the server which we are making the call to.The code for setting the cookie ... Read More

Why doesn’t Postman get a “No 'Access-ControlAllow-Origin' header is present on the requested resource” error in JavaScript

AmitDiwan
Updated on 22-Feb-2021 09:26:23

679 Views

Problem:When we try to make network request to a remote server whose origin is different to our current url (from which we are making the request), we most likely get a CORS error due to different origin problem in the web, whereas while using a tool like Postman, we can successfully avoid this CORS error.We are required to explain the difference in behaviour of response when requested through web and when requested through an extension like postman.Explanation:When we make a network request to a different domain than our page is on using a web browser, it blocks that request and ... Read More

Preview an image before it is uploaded in JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 10:26:33

7K+ Views

The ability to preview an image before it is uploaded holds significant value in the realm of web development, allowing users to have a glimpse of their selected image and make necessary adjustments prior to submission. Employing JavaScript, a versatile programming language, developers can harness the power of client-side processing to implement this functionality seamlessly. In this article, we will embark on an exploration of how to preview an image before it is uploaded using JavaScript, unveiling a step-by-step approach and leveraging seldom-utilized techniques to accomplish this task. By delving into the intricate details of this process, developers will gain ... Read More

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 an external JSON file in JavaScript

AmitDiwan
Updated on 22-Feb-2021 09:17:50

5K+ Views

Suppose we have a JSON file config.json that contains the following data −{    "secret": "sfsaad7898fdsfsdf^*($%^*$",    "connectionString":    "mongodb+srv://username:password@cluster0.laaif.mongodb.net/events?retryWrites=tr    ue&w=majority",    "localConnectionString":    "mongodb+srv://username:password@cluster0.laaif.mongodb.net/eventsLocal?retryWrit    es=true&w=majority",    "frontendClient": "https://helloworld.com",    "localFrontendClient": "http://localhost:3000" }And in the same directory (folder), we have a JavaScript file index.js.Our task is to access the content of the json file through the JavaScript file.Method 1: Using require module (NodeJS environment only)We can use the require module to access the json file if we are running our JavaScript file in NodeJS environment.ExampleThe code for this will be −const configData = require('./config.json'); console.log(typeof configData); console.log(configData);Method 2: ... Read More

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

Naveen Singh
Updated on 05-Feb-2021 11:57:40

20K+ 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

210 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

Naveen Singh
Updated on 18-May-2023 14:54:05

618 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

Advertisements