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
Object Oriented Programming Articles
Page 152 of 589
Remove the whitespaces from a string using replace() in JavaScript?
In JavaScript, the replace()
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 MoreCheck if string begins with punctuation in JavaScript
To check if a string begins with punctuation in JavaScript, we can use regular expressions to test the first character against common punctuation marks. The Problem Consider these example strings where we need to detect if they start with punctuation: var sentence1 = 'My Name is John Smith.'; var sentence2 = '? My Name is John Smith.'; console.log("Testing strings:"); console.log("1:", sentence1); console.log("2:", sentence2); Testing strings: 1: My Name is John Smith. 2: ? My Name is John Smith. Using Regular Expression with match() We can create a regular expression ...
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 MoreFinding content of arrays on the basis of specific property in JavaScript?
When working with arrays of objects in JavaScript, you often need to find and merge content based on matching properties. The find() method combined with map() provides an effective solution for this common task. Understanding the Problem Consider two arrays of student records where you want to find matching students based on their roll numbers and merge or replace data from the second array when matches are found. Using map() with find() The map() method creates a new array by transforming each element, while find() searches for the first matching element in another array. ...
Read MoreSwap kth element of array - JavaScript
We are required to write a JavaScript function that accepts an array of Numbers and a number, say k (k must be less than or equal to the length of array). And our function should replace the kth element from the beginning with the kth element from the end of the array. Understanding the Problem When we say "kth element from beginning" and "kth element from end", we need to understand the positioning: kth element from beginning: index = k - 1 kth element from end: index = array.length - k Example ...
Read MoreHow to convert an object into an array in JavaScript?
In JavaScript, there are several methods to convert an object into an array. The approach depends on whether you want the keys, values, or both from the object. Using Object.keys() to Get Keys Array The Object.keys() method returns an array of an object's property names: const student = { name: "Chris", age: 25, marks: 85, city: "New York" }; const keysArray = Object.keys(student); console.log(keysArray); [ 'name', 'age', 'marks', 'city' ] Using Object.values() to Get ...
Read MoreSquared sum of n odd numbers - JavaScript
We are required to write a JavaScript function that takes in a Number, say n, and finds the sum of the square of first n odd natural Numbers. For example, if the input number is 3, we need to find the first 3 odd numbers (1, 3, 5) and calculate the sum of their squares: 1² + 3² + 5² = 1 + 9 + 25 = 35 Understanding the Pattern The first n odd natural numbers follow the pattern: 1, 3, 5, 7, 9... The formula for the i-th odd number is (2 * i) - 1. Example const num = 3; const squaredSum = num => { let sum = 0; for(let i = 1; i
Read MoreHow to find all subsets of a set in JavaScript?
To find all subsets of a set in JavaScript, you can use the reduce() method along with map() to generate all possible combinations. A subset is any combination of elements from the original set, including the empty set and the set itself. How It Works The algorithm starts with an empty subset [[]] and for each element in the original array, it creates new subsets by adding that element to all existing subsets. Example const findAllSubsetsOfGivenSet = originalArrayValue => originalArrayValue.reduce( (givenSet, setValue) ...
Read MoreChange string based on a condition - JavaScript
We are required to write a JavaScript function that takes in a string. The task of our function is to change the string according to the following condition − If the first letter in the string is a capital letter then we should change the full string to capital letters. Otherwise, we should change the full string to small letters. Example Following is the code − const str1 = "This is a normal string"; const str2 = "thisIsACamelCasedString"; const changeStringCase = str => { ...
Read More