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
Javascript Articles
Page 388 of 534
Checking an array for palindromes - JavaScript
We are required to write a JavaScript function that takes in an array of String / Number literals and returns a subarray of all the elements that were palindrome in the original array. A palindrome is a word, number, or sequence that reads the same forward and backward. For example, "dad", "racecar", and 12321 are palindromes. Problem Statement If the input array is: const arr = ['carecar', 1344, 12321, 'did', 'cannot']; Then the output should be: const output = [12321, 'did']; Approach We will create a helper function ...
Read MoreIndent the text of a paragraph with CSS
The text-indent property is used to indent the first line of text in a paragraph or block-level element. This creates a traditional paragraph indentation effect commonly seen in books and formal documents. Syntax text-indent: value; Possible Values Length units: px, em, rem, cm, mm, in, pt Percentage: % (relative to parent element's width) Keywords: inherit, initial, unset Example: Basic Text Indentation Here's how to implement text indentation using different measurement units: ...
Read MoreSorting objects by numeric values - JavaScript
Suppose we have an object like this: const obj = { key1: 56, key2: 67, key3: 23, key4: 11, key5: 88 }; We are required to write a JavaScript function that takes in this object and returns a sorted array like this: const arr = [11, 23, 56, 67, 88]; Here, we sorted the object values and placed them in an array. Method 1: Using Object.keys() and map() This approach extracts the keys, maps them ...
Read MoreAlign the text of a paragraph with CSS
To align and control the positioning of text within a paragraph, CSS provides several properties including text-align for horizontal alignment and text-indent for first-line indentation. Text Alignment Properties The main CSS properties for text alignment are: text-align - Controls horizontal alignment (left, right, center, justify) text-indent - Indents the first line of a paragraph Using text-align Property .left { text-align: left; } .center { text-align: center; } ...
Read MoreStore count of digits in order using JavaScript
When working with strings containing digits, you often need to count how many times each digit appears. This is a common programming task that can be solved efficiently using JavaScript objects. Suppose we have a string with digits like this: const str = '11222233344444445666'; We need to write a JavaScript function that takes this string and returns an object representing the count of each digit in the string. For this string, the expected output should be: { "1": 2, "2": 4, "3": 3, "4": ...
Read MoreCapitalize text with CSS
To capitalize text in CSS, use the text-transform property with the capitalize value. This property transforms the first letter of each word to uppercase while keeping the rest lowercase. Syntax text-transform: capitalize; Basic Example Here's how to capitalize text using CSS: hello world from india Hello World From ...
Read MoreGenerating n random numbers between a range - JavaScript
We are required to write a JavaScript function that takes in a number, say n, and an array of two numbers that represents a range. The function should return an array of n random elements all lying between the range provided by the second argument. Understanding the Problem The challenge is to generate unique random numbers within a specified range. We need two helper functions: one to generate a single random number between two values, and another to collect n unique random numbers. Example Implementation Following is the code − const num = 10; ...
Read MoreUsage of white-space property in CSS
The CSS white-space property controls how whitespace characters (spaces, tabs, line breaks) are handled within an element. It determines whether text wraps, how spaces are collapsed, and whether line breaks are preserved. Syntax white-space: normal | nowrap | pre | pre-wrap | pre-line | break-spaces; White-space Values Value Spaces Collapsed? Line Breaks Honored? Text Wraps? normal Yes No Yes nowrap Yes No No pre No Yes No pre-wrap No Yes Yes pre-line Yes Yes Yes Example: Using white-space: pre ...
Read MoreWriting a For Loop to Evaluate a Factorial - JavaScript
We are required to write a simple JavaScript function that takes in a Number, say n and computes its factorial using a for loop and returns the factorial. For example: factorial(5) = 120, factorial(6) = 720 The approach is to maintain a count and a result variable, keep multiplying the count into result, simultaneously decreasing the count by 1, until it reaches 1. Then finally we return the result. Syntax function factorial(n) { let result = 1; for (let i = n; i > ...
Read MoreControl the flow and formatting of text with CSS
The white-space property is used to control the flow and formatting of text in CSS. It determines how whitespace characters (spaces, tabs, line breaks) inside an element are handled. Syntax white-space: normal | nowrap | pre | pre-wrap | pre-line; White-space Values Value Line Breaks Spaces/Tabs Text Wrapping ...
Read More