Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 445 of 840
Taking part from array of numbers by percent JavaScript
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 MoreCounting occurrences of vowel, consonants - JavaScript
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 MoreJavaScript encodeURI(), decodeURI() and its components functions
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 MoreHow to close list items with JavaScript?
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 MoreHow to output JavaScript into a Textbox?
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 MoreProduct of all other numbers an array in JavaScript
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 MoreDisplay the dropdown's (select) selected value on console in JavaScript?
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 MoreChecking progressive array - JavaScript
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 MoreSorting an array object by property having falsy value - JavaScript
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 MoreGet global variable dynamically by name string in JavaScript?
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