Found 8591 Articles for Front End Technology

Converting 12 hour Format Time to 24 hour Format in JavaScript

AmitDiwan
Updated on 20-Jan-2025 12:35:40

10K+ Views

Converting 12 hour format time to 24 hour format in JavaScript can be easily achieved using various approaches which we will be discussing in this article. For conversion from 12 hour to 24 hour format we check for the modifier(AM or PM). Depending on the modifier we convert the time formats. In this article, we have specified a time at the beginning in 12 hour format. Our task is to convert 12 hour format time to 24 hour format in JavaScript. Approaches to Convert 12-hours format to 24-hours Here is a list of approaches to convert 12 hour format ... Read More

Removing the odd occurrence of any number/element from an array in JavaScript

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

285 Views

Suppose, we have an array of numbers like this −const arr = [1, 6, 3, 1, 3, 1, 6, 3];We are required to write a JavaScript function that takes in one such array as the first and the only argument. Then the function should look for all such numbers in the array that appear for an odd number of times (excluding only once).For example, In the above array, the numbers 1 and 3 both appear for 3 times (odd), so our function should remove the third occurrence of both these numbers.And the output array should look like −const output = ... Read More

Flattening a JSON object in JavaScript

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

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

310 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

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

741 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

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

How to Create a Dark / Light Mode for Websites using CSS

AmitDiwan
Updated on 10-Feb-2021 13:59:59

728 Views

By changing the text-color and background color of a page, we can add dark/light mode for our website.SyntaxThe following syntax can be used to apply dark mode.Selector {    color: white;    background-color: black }Example Live Demo                    div {             font-size: 1.4em;             text-align: center;          }          .dark {             color: white;             background-color: black;          }       ... Read More

Advertisements