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
Front End Technology Articles
Page 367 of 652
Converting days into years months and weeks - JavaScript
We are required to write a JavaScript function that takes in a number (representing the number of days) and returns an object with four properties: years, months, weeks, days The properties should have proper values that can be made from the total number of days. We assume a standard year has 365 days and each month has 30 days for simplicity. Problem Example If the input is 738 days, the output should be: { years: 2, months: 0, weeks: 1, days: 1 } ...
Read MoreCheck Disarium number - JavaScript
A Disarium number is a number where the sum of its digits raised to their respective positions equals the original number. Definition For a number with digits xy...z, it's a Disarium number if: xy...z = x^1 + y^2 + ... + z^n Where n is the total number of digits in the number. Example Let's check if 175 is a Disarium number: 175 = 1^1 + 7^2 + 5^3 = 1 + 49 + 125 = 175 Since the sum equals the original number, 175 is a Disarium ...
Read MoreCount total punctuations in a string - JavaScript
In JavaScript, you can count punctuation marks in a string by checking each character against a set of punctuation symbols. Common punctuation marks include periods, commas, semicolons, exclamation marks, question marks, and quotation marks. Common Punctuation Characters '!', ", ", "'", ";", '"', ".", "-", "?" Example Here's a JavaScript function that counts punctuation marks in a string: const str = "This, is a-sentence;.Is this a sentence?"; const countPunctuation = str => { const punct = "!, ';".?-"; let count = 0; ...
Read MoreRemove all whitespaces from string - JavaScript
We are required to write a JavaScript function that takes in a string and returns a new string with all the characters of the original string but with whitespaces removed. Method 1: Using a For Loop This approach iterates through each character and builds a new string excluding spaces: const str = "This is an example string from which all whitespaces will be removed"; const removeWhitespaces = str => { let newStr = ''; for(let i = 0; i < str.length; i++){ ...
Read MoreReplacing upperCase and LowerCase in a string - JavaScript
We are required to write a JavaScript function that takes in a string and constructs a new string with all the uppercase characters converted to lowercase and all the lowercase characters converted to uppercase. Let's write the code for this function — Example Following is the code — const str = 'The Case OF tHis StrinG Will Be FLiPped'; const isUpperCase = char => char.charCodeAt(0) >= 65 && char.charCodeAt(0) char.charCodeAt(0) >= 97 && char.charCodeAt(0) { let newStr = ''; const margin = 32; ...
Read MoreFinding duplicate "words" in a string - JavaScript
We need to write a JavaScript function that takes a string and returns only the words that appear more than once in the original string. For example, if the input string is: const str = "big black bug bit a big black dog on his big black nose"; Then the output should be: "big black" Solution Using indexOf() and lastIndexOf() We can identify duplicates by comparing the first and last occurrence positions of each word: const str = "big black bug bit a big black dog on his ...
Read MoreFinding roots of a quadratic equation – JavaScript
A quadratic equation has the form ax² + bx + c = 0, where a, b, and c are coefficients. To find the roots, we use the quadratic formula and check if the discriminant is non-negative for real roots. Quadratic Formula The quadratic formula is: x = (-b ± √(b² - 4ac)) / (2a) The discriminant (b² - 4ac) determines the nature of roots: If discriminant > 0: Two distinct real roots If discriminant = 0: One repeated real root If discriminant < 0: No real roots (complex roots) Example ...
Read MoreNearest power 2 of a number - JavaScript
We need to write a JavaScript function that takes a number and returns the nearest power of 2. A power of 2 is any number that can be expressed as 2n where n is a whole number (1, 2, 4, 8, 16, 32, 64, 128, 256, etc.). For example, if the input is 365, the output should be 256 because 256 (28) is closer to 365 than the next power of 2, which is 512 (29). Algorithm Explanation The algorithm works by: Starting with base = 1 (20) Doubling the base until it exceeds the input ...
Read MoreFinding area of triangle in JavaScript using Heron's formula
We are given the lengths of three sides of a triangle and we are required to write a function that returns the area of the triangle using the length of its sides. Heron's Formula We can calculate the area of a triangle if we know the lengths of all three sides, using Heron's formula: Step 1 − Calculate "s" (half of the triangle's perimeter): s = (a + b + c) / 2 Step 2 − Then calculate the Area using Heron's formula: A = √(s(s-a)(s-b)(s-c)) Syntax ...
Read MoreSplit string into groups - JavaScript
Given a string that consists of alphabets, numbers, and special characters, we need to split it into three separate strings based on character type. We'll create three strings where: S1 contains all alphabets from the original string S2 contains all numbers from the original string S3 contains all special characters from the original string The characters in each resulting string maintain the same order as they appear in the input string. Example Implementation const str = "Th!s String C0nt@1ns d1fferent ch@ract5rs"; const ...
Read More