Object Oriented Programming Articles

Page 104 of 589

Inverse operation in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 994 Views

The inverse operation on a binary string involves flipping each bit: converting all 0s to 1s and all 1s to 0s. This is a common operation in computer science and digital logic. Understanding Binary Inverse Binary inverse (also called bitwise NOT or complement) transforms each digit in a binary string to its opposite value. For example, '1101' becomes '0010'. Method 1: Using Array Methods We can split the string into individual characters, transform each one, and join them back: const num = '1101'; const n = '11010111'; const inverseBinary = (binary) => { ...

Read More

How to add a property, method to a JavaScript constructor?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 289 Views

In JavaScript, you can add properties and methods to constructor functions using the prototype property. This allows all instances of the constructor to share these additions. Syntax // Add a property ConstructorName.prototype.propertyName = value; // Add a method ConstructorName.prototype.methodName = function() { // method body }; Example: Adding Property and Method to Constructor Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } ...

Read More

Convert JavaScript array iteration result into a single line text string

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 355 Views

Let's say we have a string and an array of keywords to search for: const textString = 'Convert javascript array iteration result into a single line text string. Happy searching!'; const keywords = ['integer', 'javascript', 'dry', 'Happy', 'exam']; We need to write a function that maps the array to a string containing only true and false values, depending on whether each array element is present in the string or not. Solution Using reduce() and join() The most efficient approach uses reduce() to build an array of boolean values, then join() to convert it into ...

Read More

Creating JavaScript constructor using the "new" operator?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 209 Views

In JavaScript, constructor functions create objects using the new operator. A constructor function is a regular function that becomes a template for creating multiple objects with similar properties. Syntax function ConstructorName(param1, param2) { this.property1 = param1; this.property2 = param2; } // Create new object let obj = new ConstructorName(value1, value2); Example JavaScript Constructor body ...

Read More

Append the current array with the squares of corresponding elements of the array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 166 Views

We have an array of Numbers like this: const arr = [12, 19, 5, 7, 9, 11, 21, 4]; We have to write a function that takes in such an array and returns a new array with all the items of the original array appended by the squares of corresponding elements of the array. For this sample array, the output should be: [12, 19, 5, 7, 9, 11, 21, 4, 144, 361, 25, 49, 81, 121, 441, 16] Method 1: Using reduce() The reduce() method can accumulate the original array ...

Read More

Returning values from a constructor in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 235 Views

In JavaScript, constructors typically don't need explicit return statements since they automatically return the newly created object. However, you can override this behavior by explicitly returning an object from a constructor. How Constructor Returns Work When you use the new keyword with a constructor: If the constructor returns an object, that object becomes the result If the constructor returns a primitive value (string, number, boolean), it's ignored and this is returned instead If there's no explicit return, this is returned automatically Example: Returning an Object from Constructor ...

Read More

Adding only odd or even numbers JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 990 Views

We are required to make a function that given an array of numbers and a string that can take any of the two values "odd" or "even", adds the numbers which match that condition. If no values match the condition, 0 should be returned. For example: conditionalSum([1, 2, 3, 4, 5], "even") => 6 conditionalSum([1, 2, 3, 4, 5], "odd") => 9 conditionalSum([13, 88, 12, 44, 99], "even") => 144 conditionalSum([], "odd") => 0 Using Array.reduce() Method We'll use the Array.prototype.reduce() method to iterate through the array and sum numbers based on the condition: ...

Read More

Explain the finally Statement in JavaScript with examples.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 250 Views

The finally statement in JavaScript always executes after try and catch blocks, regardless of whether an error occurred or not. This makes it ideal for cleanup operations and code that must run in all scenarios. Syntax try { // Code that may throw an error } catch (error) { // Handle error (optional) } finally { // Always executes } Example: Finally Block Always Executes ...

Read More

Make array numbers negative JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 971 Views

In JavaScript, you can convert all positive numbers in an array to negative while keeping already negative numbers unchanged. This is useful for data transformations where you need to invert positive values. Let's say we have the following array: const arr = [7, 2, 3, 4, 5, 7, 8, 12, -12, 43, 6]; We need to write a function that converts all positive numbers to negative while leaving negative numbers unchanged (like 4 to -4, 6 to -6, but -12 stays -12). Using Array.reduce() const arr = [7, 2, 3, 4, 5, ...

Read More

What is JavaScript type coercion?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 285 Views

Type coercion in JavaScript refers to the automatic conversion of values from one data type to another. This happens implicitly when JavaScript needs to perform operations between different types. How Type Coercion Works JavaScript automatically converts types in certain situations, such as when using operators or comparing values. This can sometimes lead to unexpected results if you're not familiar with the coercion rules. String Coercion Example Type Coercion Example JavaScript Type Coercion ...

Read More
Showing 1031–1040 of 5,881 articles
« Prev 1 102 103 104 105 106 589 Next »
Advertisements