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 489 of 840
Count total punctuations in a string - JavaScript
In JavaScript, you can count punctuation marks in a string by checking each character against a set of punctuation symbols. Common punctuation marks include periods, commas, semicolons, exclamation marks, question marks, and quotation marks. Common Punctuation Characters '!', ", ", "'", ";", '"', ".", "-", "?" Example Here's a JavaScript function that counts punctuation marks in a string: const str = "This, is a-sentence;.Is this a sentence?"; const countPunctuation = str => { const punct = "!, ';".?-"; let count = 0; ...
Read MoreFinding the maximum in a nested array - JavaScript
Let's say, we have to write a simple function in JavaScript that takes in the following array of Numbers (nested to any level) − const arr = [ 15, 24, [ 29, 85, 56, [ 36, 14, 6, 98, 34, 52 ], 22 ], 87, 60 ]; and return the greatest number present in ...
Read MoreJavaScript Determine the array having majority element and return TRUE if its in the same array
We are required to write a JavaScript function that takes in an array of numbers with repetitive values and returns the number that appears for more than (n/2) number of times where n is the length of the array. If there is no such element in the array, our function should return false. This problem is commonly known as finding the "majority element" in an array. A majority element is one that appears more than half the time in the array. Algorithm Approach We'll use the Boyer-Moore Voting Algorithm, which works in two phases: ...
Read MoreMethod to check if array element contains a false value in JavaScript?
To check if an array element contains a false value in JavaScript, you can use methods like some(), includes(), or Object.values() depending on your data structure. Using some() for Simple Arrays The some() method tests whether at least one element passes a test function: const booleanArray = [true, false, true]; const hasfalseValue = booleanArray.some(value => value === false); console.log("Array contains false:", hasfalseValue); // Or more simply const hasFalse = booleanArray.includes(false); console.log("Using includes():", hasFalse); Array contains false: true Using includes(): true Using Object.values() for Nested Objects For complex nested structures, ...
Read MoreImplement divide & conquer logic in JavaScript to implement QuickSort
We are required to write a JavaScript function that takes in an array of numbers and uses the quick sort algorithm to sort it. QuickSort Algorithm QuickSort is a divide and conquer algorithm that works by selecting a 'pivot' element from the array and partitioning other elements into two sub-arrays according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively. How It Works The algorithm follows these steps: Choose a pivot element (typically the middle element) Partition the array so elements smaller than pivot go to the ...
Read MoreRemoving identical entries from an array keeping its length same - JavaScript
We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end. For example − If we find four duplicate values, we have to remove them all and insert four empty strings at the end. Understanding the Problem The goal is to: Remove duplicate elements from the array Keep only the last occurrence of each element Maintain the original array length by adding empty strings Example ...
Read MoreCombining multiple images into a single one using JavaScript
JavaScript's Canvas API allows you to combine multiple images into a single composite image. This technique is useful for creating overlays, watermarks, or blending effects in web applications. How Canvas Image Combining Works The process involves loading images into a canvas element and using the drawImage() method with different alpha transparency values to create layered effects. Example: Overlaying Two Images Combining Multiple Images body { ...
Read MoreCheck if input is a number or letter in JavaScript?
JavaScript provides several methods to check if an input is a number or letter. The most common approach uses the isNaN() function, which returns true if the value is "Not a Number". Using isNaN() Function The isNaN() function converts the input to a number and checks if it's NaN (Not a Number): Check Number or Letter Enter the value: ...
Read MoreToggle hide class only on selected div with JavaScript?
To toggle hide class only on selected div, you need to set event on click button. Let's say you need to hide a specific div on the click of + sign. To get font + or - icon, you need to link font-awesome: HTML Structure The HTML contains multiple customer sections, each with a toggle button that can show/hide specific content: Toggle Hide Class Example ...
Read MoreRemove all whitespaces from string - JavaScript
We are required to write a JavaScript function that takes in a string and returns a new string with all the characters of the original string but with whitespaces removed. Method 1: Using a For Loop This approach iterates through each character and builds a new string excluding spaces: const str = "This is an example string from which all whitespaces will be removed"; const removeWhitespaces = str => { let newStr = ''; for(let i = 0; i < str.length; i++){ ...
Read More