AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 445 of 840

Taking part from array of numbers by percent JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

We have an array of number literals like this: const numbers = [10, 6200, 20, 20, 350, 900, 26, 78, 888, 10000, 78, 15000, 200, 1280, 2000, 450]; We need to write a function that takes an array of numbers and a percentage (0-100). The function should return the first n elements from the array that sum up to equal or just less than the specified percentage of the total array sum. Understanding the Problem Let's take a simpler example: const numbers = [12, 10, 6, 8, 4, 2, 8]; ...

Read More

Counting occurrences of vowel, consonants - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 596 Views

We are required to write a JavaScript function that takes in a string which contains English alphabet, for example − const str = 'This is a sample string, will be used to collect some data'; The function should return an object containing the count of vowels and consonants in the string i.e. the output should be − { vowels: 17, consonants: 29 } Understanding the Problem To count vowels and consonants, we need to: Iterate through each character in the string Check if the character ...

Read More

JavaScript encodeURI(), decodeURI() and its components functions

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 381 Views

JavaScript provides four essential functions for handling URI encoding and decoding. The encodeURI() function encodes complete URIs while preserving structural characters, whereas encodeURIComponent() encodes URI components including all special characters. Understanding the Functions The encodeURI() function encodes special characters in a URI except for reserved characters like , / ? : @ & = + $ # which are essential for URI structure. The encodeURIComponent() function encodes URI components by encoding all special characters including the reserved ones. The corresponding decode functions decodeURI() and decodeURIComponent() reverse the encoding process. Syntax encodeURI(uri) decodeURI(encodedURI) encodeURIComponent(uriComponent) decodeURIComponent(encodedURIComponent) ...

Read More

How to close list items with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 655 Views

Closing list items with JavaScript involves adding click event listeners to close buttons that hide or remove list items from the DOM. This creates an interactive list where users can dismiss items they no longer need. Complete Example body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } * { ...

Read More

How to output JavaScript into a Textbox?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 5K+ Views

You can output JavaScript values to a textbox using the value property. This is commonly done to display calculation results or user feedback. Basic Approach To output to a textbox, access the input element and set its value property: document.getElementById("myTextbox").value = "Your output here"; Example: Calculator with Textbox Output JavaScript Textbox Output First Number: ...

Read More

Product of all other numbers an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 393 Views

Let's say, we have to write a function that takes an array of numbers as argument. We have to return a new array with the products of each number except the index we are currently calculating product for. For example, if arr had 5 indices and we were creating the value for index 1, the numbers at index 0, 2, 3 and 4 would be multiplied. Similarly, if we were creating the value for index 2, the numbers at index 0, 1, 3 and 4 would be multiplied and so on. Note − It is guaranteed that all ...

Read More

Display the dropdown's (select) selected value on console in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 3K+ Views

In JavaScript, you can capture the selected value from a dropdown (select element) and display it in the console using the onchange event and console.log(). HTML Structure Here's a basic dropdown with an onchange event handler: Javascript MySQL MongoDB Java Complete Example Dropdown Selected Value Select a Subject: ...

Read More

Checking progressive array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 253 Views

We are required to write a JavaScript function that takes in an array of strings, ordered by ascending length. The function should return true if, for each pair of consecutive strings, the second string can be formed from the first by adding a single letter either at the beginning or end. For example: If the array is given by − const arr = ["c", "ca", "can", "acan", "acane", "dacane"]; Then our function should return true. How It Works The algorithm checks each consecutive pair of strings to verify that the longer string ...

Read More

Sorting an array object by property having falsy value - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 274 Views

Suppose, we have an array of objects like this − const array = [ {key: 'a', value: false}, {key: 'a', value: 100}, {key: 'a', value: null}, {key: 'a', value: 23} ]; We are required to write a JavaScript function that takes in one such array and places all the objects that have falsy values for the "value" property to the bottom and sorts all other objects in decreasing order by the "value" property. Understanding Falsy Values In JavaScript, falsy values include false, null, undefined, 0, "", ...

Read More

Get global variable dynamically by name string in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 541 Views

In JavaScript, you can access global variables dynamically using their name as a string through the window object (in browsers) or global object (in Node.js). Syntax // Browser environment window[variableName] // Node.js environment global[variableName] // Using globalThis (works in both) globalThis[variableName] Basic Example Dynamic Global Variables // Define global variables ...

Read More
Showing 4441–4450 of 8,392 articles
« Prev 1 443 444 445 446 447 840 Next »
Advertisements