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
Web Development Articles
Page 182 of 801
Number of digits that divide the complete number in JavaScript
We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should count and return the number of digits present in the number that completely divide the number. For example, if the input number is 148, the output should be 2 because 148 is exactly divisible by 1 and 4 but not 8. Example Input and Output Input: 148 Output: 2 This is because: 148 ÷ 1 = 148 (divisible) 148 ÷ 4 = 37 (divisible) ...
Read MoreDetermining a pangram string in JavaScript
A pangram is a string that contains every letter of the English alphabet at least once. Examples include "The quick brown fox jumps over the lazy dog" and "Pack my box with five dozen liquor jugs". We need to write a JavaScript function that determines whether a given string is a pangram. For this problem, we'll focus on the 26 letters of the English alphabet, ignoring case sensitivity. How It Works The algorithm creates an array of all 26 letters, then iterates through the input string. For each letter found in the string, it removes that letter ...
Read MoreHow do I console.log JavaScript variables as it relates to DOM?
To display variables on console when working with the DOM, you can use console.log() to output element objects, their properties, or values. The document.getElementById() method retrieves DOM elements which can then be logged to examine their properties. Logging DOM Elements When you log a DOM element directly, the browser console displays the element object with all its properties: Console Log DOM Example Hello World ...
Read MoreCalculating the weight of a string in JavaScript
In JavaScript, calculating the weight of a string means finding the sum of alphabetical positions of all characters. Each letter's weight is its position in the alphabet (a=1, b=2, c=3, etc.). Understanding Character Weight The weight of an English alphabet character is its 1-based index position: 'a' has weight 1 'b' has weight 2 'c' has weight 3 'z' has weight 26 Method 1: Using indexOf() This approach uses a reference string to find each character's position: const str = 'this is a string'; const calculateWeight = (str = '') ...
Read MoreAdd property to common items in array and array of objects - JavaScript?
To add properties to objects based on common items in arrays, use the map() method combined with Set for efficient lookups. This approach allows you to conditionally add properties to array objects based on whether their values exist in another array. Example Arrays Let's start with a simple array and an array of objects: const firstname = ['John', 'David', 'Bob']; const studentDetails = [ { firstname: 'Carol', marks: 78 }, ...
Read More2 Key keyboard problem in JavaScript
In this problem, we start with a notepad containing one character 'A' and need to reach exactly 'n' characters using only two operations: Copy All and Paste. The goal is to find the minimum number of steps required. Problem Understanding Given a notepad with one 'A', we can perform: Copy All − Copy all characters currently on the notepad Paste − Paste the previously copied characters We need to find the minimum steps to get exactly 'n' A's on the notepad. Example Walkthrough For ...
Read MoreHow to edit values of an object inside an array in a class - JavaScript?
In JavaScript, you can edit values of objects inside an array within a class by using the this keyword. This allows you to reference and modify properties of objects stored in class arrays. Understanding the Structure When working with objects inside arrays in classes, each object can contain its own methods that modify its properties. The key is understanding how this refers to the current object context. Example Here's how to create a class with an array of objects and modify their values: class Employee { constructor() { ...
Read MoreSquaring every digit of a number using split() in JavaScript
We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should then square every digit of the number, append them and yield the new number. For example − If the input number is − const num = 12349; Then the output should be − const output = 1491681; because '1' + '4' + '9' + '16' + '81' = 1491681 How It Works The solution uses split('') to convert each digit into an array element, then ...
Read MoreHow to allocate memory to an object whose length is set to 0 - JavaScript?
In JavaScript, you can manipulate array length to clear memory and allocate new slots. When you set an array's length to 0, it clears all elements. Setting length to a larger value allocates empty slots. Understanding Array Length Property The length property controls how many elements an array can hold. Setting it to 0 removes all elements, while setting it to a larger number creates empty slots filled with undefined. var arrayObject = [ "John", "David", "Mike" ]; console.log("Original array:", arrayObject); console.log("Original ...
Read MoreCalculating time taken to type words in JavaScript
In JavaScript, we can calculate the time taken to type words on a custom keyboard where keys are arranged alphabetically (a-z) instead of the traditional QWERTY layout. Problem Setup We make two key assumptions: The fingertip starts at index 0 (key 'a') Time to move between keys equals the absolute difference of their positions. For example, moving from 'a' (index 0) to 'k' (index 10) takes |0 - 10| = 10 time units Example Walkthrough For the string 'dab', the movements are: 'a' ...
Read More