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 on Trending Technologies
Technical articles with clear explanations and examples
Summing numbers from a string - JavaScript
We are required to write a JavaScript function that takes in a string that contains some one-digit numbers in between and the function should return the sum of all the numbers present in the string. Let's say the following is our string with numbers: const str = 'gdf5jhhj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3'; Method 1: Using for Loop with Type Conversion This approach splits the string into characters and checks each one using type conversion: const str = 'gdf5jhhj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3'; const sumStringNum = str => { const strArr = str.split(""); let ...
Read MoreJavaScript - Writing a string function to replace the kth appearance of a character
Let's say, we are required to write a String.prototype function that takes in three arguments. First argument is string that should be searched for substrings Second argument is the string, the occurrence of which String to be removed Third argument is a Number say n, nth occurrence of substring to be removed from string. The function should return the new string if the removal of the subStr from the string was successful, otherwise it should return -1 in all cases. Example Following is the code − const str = 'jkdsttjkdsre'; const subStr ...
Read MoreHow to create a session only cookies with JavaScript?
Session cookies are temporary cookies that expire when the browser session ends (when the user closes the browser). Unlike persistent cookies, they don't have an expiration date set and are automatically deleted when the browser is closed. Creating Session Cookies To create a session cookie in JavaScript, simply omit the expires or max-age attribute when setting the cookie: // Session cookie - no expiration date document.cookie = "sessionUser=johnDoe; path=/"; // Another session cookie with additional attributes document.cookie = "tempData=someValue; path=/; secure; samesite=strict"; console.log("Session cookies created"); console.log("Current cookies:", document.cookie); Session ...
Read MoreSet the font size with CSS
The font-size property in CSS controls the size of text in your web pages. You can specify font sizes using various units and keyword values to create visual hierarchy and improve readability. Syntax font-size: value; Font Size Values The font-size property accepts several types of values: Keywords: xx-small, x-small, small, medium, large, x-large, xx-large Relative keywords: smaller, larger Units: pixels (px), percentages (%), em, rem Example: Different Font Size Values Font Size Examples This font ...
Read MoreHow to separate alphabets and numbers from an array using JavaScript
We have an array that contains some number literals and some string literals mixed, and we are required to write a sorting function that separates the two and the two types should also be sorted within. This approach uses a custom comparator function with JavaScript's sort() method to first separate numbers from strings, then sort each type individually. Using Custom Sort Comparator The code for this sorting function will be: const arr = [1, 5, 'fd', 6, 'as', 'a', 'cx', 43, 's', 51, 7]; const sorter = (a, b) => { ...
Read MoreNearest palindrome in JavaScript
We are required to write a function that takes in a number n and returns a palindromic number that is nearest to the number n. A palindrome reads the same forwards and backwards. For example: If the input number is 264, then the output should be 262 If the input number is 7834, then the output should be 7887 Approach The approach divides the number into two halves based on its length and creates a palindrome by mirroring the first half. For odd-length numbers, the middle digit remains unchanged. Example const ...
Read MoreHow do I display the content of a JavaScript object in a string format?
To display JavaScript object content as a string, you have several methods. The most common approach is using JSON.stringify() which converts objects into readable JSON format. Using JSON.stringify() The JSON.stringify() method converts JavaScript objects into JSON strings: Display JavaScript Object // Create a JavaScript object var ...
Read MoreSwitch case calculator in JavaScript
Let's say, we are required to write a JavaScript function that takes in a string like these to create a calculator − "4 add 6" "6 divide 7" "23 modulo 8" Basically, the idea is that the string will contain two numbers on either sides and a string representing the operation in the middle. The string in the middle can take one of these five values − "add", "divide", "multiply", "modulo", "subtract" Our job is to return the correct result based on the string Syntax switch (expression) { ...
Read MoreReplacing all special characters with their ASCII value in a string - JavaScript
We are required to write a JavaScript function that takes in a string that might contain some special characters. The function should return a new string with all special characters replaced with their corresponding ASCII value. Understanding Special Characters Special characters are symbols that are not letters, numbers, or spaces. Examples include !, @, #, $, etc. Each character has a unique ASCII code that can be retrieved using the charCodeAt() method. Example Following is the code: const str = 'Th!s !s @ str!ng th@t cont@!ns some special characters!!'; const specialToASCII = str ...
Read MoreHow to set the type of positioning method used for an element with JavaScript?
In this tutorial, we shall learn to set the type of positioning method used for an element with JavaScript. The positioning permits us to move elements out of actual document order and make them appear in various ways. For instance, stay on top of one another or occupy the initial position within the browser viewport. Understanding CSS Position Types Before diving into JavaScript implementation, let's understand the available position values: static − The default position. Elements follow normal document flow. relative − Positioned relative to its normal position without affecting other elements. absolute − Positioned ...
Read More