Aligning images on a card with dynamic titles in JavaScript involves using CSS for layout and JavaScript for dynamic content updates. This creates responsive, interactive card components commonly used in modern web applications. Approach Create a card container using a div or section element with appropriate CSS classes. Add a header element for the dynamic title with a unique class or ID for styling and JavaScript targeting. Include an image element within the card container using the img tag or background image. Use CSS Flexbox properties like display: flex, justify-content, and align-items to control alignment. Apply responsive ... Read More
We are required to write a JavaScript function that takes in a string and a number, say k and returns another string with first k characters removed from the string. For example: If the original string is − const str = "this is a string" and, n = 4 then the output should be − const output = " is a string" Method 1: Using substr() Method The substr() method extracts characters from a string starting at a specified position. const str = 'this ... Read More
We are required to write a function that, given a number, say, 123, will output an array − [100, 20, 3] Basically, the function is expected to return an array that contains the place value of all the digits present in the number taken as an argument by the function. Understanding Place Values Each digit in a number has a place value based on its position. For example, in 123: 1 is in the hundreds place (1 × 100 = 100) 2 is in the ... Read More
We are required to write a JavaScript function that takes in an array of integers and sorts it using recursion with stack operations (push and pop methods). The function sorts the array in-place without using built-in sorting methods. How It Works The algorithm uses two recursive functions: sortStack() - Removes elements from the stack and recursively sorts the remaining elements sortedInsert() - Inserts an element in its correct position in an already sorted stack Example Here's the complete implementation: const stack = [-3, 14, 18, -5, 30]; const sortStack = (stack ... Read More
We are required to write a JavaScript function that takes in two numbers, let's say m and n as arguments. n will always be smaller than or equal to the number of digits present in m. The function should calculate and return the sum of first n digits of m. Problem Statement If the input numbers are: const m = 5465767; const n = 4; Then the output should be: 20 because 5 + 4 + 6 + 5 = 20 Solution Using String Conversion The most ... Read More
In this article, we will check whether a number lies between a range or not and display a message according to the output we get. This feature of JavaScript allows you to put a number validation while creating a form or any other document. Syntax Following is the syntax to check if number is in range and display the message − if (isNaN(number) || number < lower || number > upper) { document.getElementById("output").innerHTML = number + " is not in range"; } else { document.getElementById("output").innerHTML = number + " is ... Read More
We can align text content into the center using JavaScript by manipulating the CSS properties of HTML elements. This is done by selecting the target element and setting the "text-align" CSS property to "center" using JavaScript's style object. Methods to Center Text There are multiple approaches to center text content using JavaScript: Setting the textAlign property to "center" via JavaScript's style object Using margin properties with auto values for horizontal centering Dynamically creating centered elements with predefined styles Method 1: Using textAlign Property The ... Read More
The 'heap out of memory' error occurs when JavaScript code exceeds the allocated memory limit. This commonly happens with inefficient algorithms that have high time or space complexity, especially when processing large numbers. When finding prime factors of very large numbers, naive algorithms can quickly exhaust available memory. The key is optimizing the algorithm to reduce both time and space complexity. The Problem: Inefficient Prime Factor Algorithm The following example demonstrates how a poorly optimized algorithm causes memory issues when processing large numbers: Visualizing Memory Error with Large Numbers ... Read More
We are required to write a JavaScript function that takes in two numbers, say, a and b and returns the total number of prime numbers between a and b (including a and b, if they are prime). For example: If a = 21, and b = 38. The prime numbers between them are 23, 29, 31, 37 And their count is 4 Our function should return 4 Understanding Prime Numbers A prime number is a natural number greater than 1 that has ... Read More
Reverse mapping an object in JavaScript involves swapping the keys and values of an object. This technique is useful when you need to look up keys based on their values. Suppose we have an object like this: const products = { "Pineapple": 38, "Apple": 110, "Pear": 109 }; All the keys are unique in themselves and all the values are unique in themselves. We need to write a function that accepts a value and returns its corresponding key. Method 1: Using Object.keys() with map() ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance