AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 376 of 840

String to binary in JavaScript

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

Converting strings to binary representation is a common task in JavaScript programming. This involves converting each character to its ASCII code and then to binary format. For example, if we have: const str = 'Hello World'; The binary output should be: 1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100 How It Works The conversion process involves three steps: Split the string into individual characters Get the ASCII code of each character using charCodeAt() ...

Read More

Counting specific digits used in squares of numbers using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 411 Views

We need to write a JavaScript function that takes an integer n (n >= 0) and a digit d (0

Read More

Finding sum of every nth element of array in JavaScript

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

We are required to write a JavaScript function that takes in an array of numbers and returns the cumulative sum of every number present at the index that is a multiple of n from the array. Understanding the Problem When we say "every nth element", we mean elements at indices 0, n, 2n, 3n, and so on. For example, if n=3, we sum elements at indices 0, 3, 6, 9, etc. Example Let's find the sum of every 3rd element from an array: const arr = [5, 3, 5, 6, 12, 5, 65, 3, ...

Read More

Sorting array according to increasing frequency of elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 403 Views

We are required to write a JavaScript function that takes in an array of literals as the first and the only argument. The array is likely to contain many repeating values. Our function should sort the array such that the values that are unique or that have the least frequency are placed before the ones that have the most. For example − If the input array is − const arr = [4, 7, 3, 5, 5, 4, 7, 9, 2, 1, 5, 7, 5, 5, 9]; Then the output array should be − ...

Read More

Finding length, width and height of the sheet required to cover some area using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 333 Views

Problem Statement We need to write a JavaScript function that calculates the number of wallpaper sheets required to cover a room's walls. Given the room's length, width, and height, we must determine how many sheets are needed when each sheet has dimensions of 0.52 units width and 10 units length. The function should return 15% more sheets than the calculated requirement to account for waste and cutting. Understanding the Calculation The total wall area of a rectangular room is calculated using the formula: 2 × (length + width) × height. This covers all four walls, excluding ...

Read More

How to adjust the Width of Input Field Automatically using JavaScript?

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

We can use JavaScript to adjust the width of an input field automatically. This can be done by using the element's style property to set the width based on the input's value. By constantly updating the width as the user types, we can ensure that the input field is always the appropriate size for the input. Basic Approach Here is one approach to adjust the width of an input field automatically using JavaScript: Select the input field using JavaScript, for example, by using the document.querySelector() method: var inputField = document.querySelector("#myInput"); ...

Read More

How to remove some items from array when there is repetition in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 153 Views

In JavaScript, you may need to remove items from an array that appear in multiples of three (triplets). This is useful when you want to keep only the remaining elements after removing complete sets of triplets. Understanding the Problem The goal is to remove triplets (groups of 3 identical elements) from an array and keep only the remaining elements. For example, if an array has 5 occurrences of the number 1, we remove 3 of them (one triplet) and keep 2. Solution Implementation const arr1 = [1, 1, 1, 3, 3, 5]; const arr2 = ...

Read More

Check for perfect square in JavaScript

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

We are required to write a JavaScript function that takes in a number and returns a boolean based on the fact whether or not the number is a perfect square. A perfect square is a number that can be expressed as the product of an integer with itself. For example, 16 is a perfect square because 4 × 4 = 16. Examples of Perfect Square Numbers 144 (12²), 196 (14²), 121 (11²), 81 (9²), 484 (22²) Method 1: Using Math.sqrt() The most straightforward approach is to find the square root and check if ...

Read More

Finding perfect numbers in JavaScript

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

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly. For example: 28 is a perfect number, because 28 = 1 + 2 + 4 + 7 + 14 We are required to write a JavaScript function that takes in a number, say n, and determines whether or not n is a perfect number. How Perfect Numbers Work To check if a number is perfect, we need to: ...

Read More

Implementing Heap Sort using vanilla JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 303 Views

Heap Sort is a comparison-based sorting algorithm that can be thought of as an improved selection sort. Like selection sort, it divides input into sorted and unsorted regions, but uses a binary heap data structure to efficiently find the maximum (or minimum) element. How Heap Sort Works Heap Sort works in two main phases: Build Max Heap: Convert the array into a max heap where parent nodes are larger than their children Extract Elements: Repeatedly remove the maximum element (root) and restore the heap property Implementation Here's a complete implementation of Heap Sort ...

Read More
Showing 3751–3760 of 8,392 articles
« Prev 1 374 375 376 377 378 840 Next »
Advertisements