Object Oriented Programming Articles

Page 122 of 589

Get n numbers from array starting from given point JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 325 Views

We need to create a custom Array method that extracts n elements from an array starting at a given index, moving in a specified direction (left or right). When we reach array boundaries, the function wraps around to continue from the other end. Problem Requirements The function should take three parameters: n - number of elements to extract m - starting index (must be ≤ array.length - 1) direction - either 'left' or 'right' For example, if we have [0, 1, 2, 3, 4, 5, 6, 7] and call get(4, 6, 'right'), it should return ...

Read More

Dot notation in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

Dot notation is the most common way to access object properties in JavaScript. It uses a dot (.) between the object name and property name to retrieve or set values. Syntax objectName.propertyName Basic Example Dot Notation Example Student Information Show Student Details function Student(name, age, standard) { ...

Read More

How to compare two arrays to see how many same elements they have in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 457 Views

Let's say, we have two arrays, one contains the correct answer strings of some questions and one contains the answers attempted by a candidate, but somehow the arrays got shuffled and now they don't have answers in corresponding order. But we can be sure that no two questions had the same answers. Our job now is to write a function that takes these two arrays, checks them for common elements and finds all the common elements between them and then calculates the marks percentage of the candidate based on the count of common answers. Example const ...

Read More

Dot notation vs Bracket notation in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

The dot notation and bracket notation are both methods for accessing object properties in JavaScript. While dot notation is cleaner and more commonly used, bracket notation provides greater flexibility, especially when working with dynamic property names or property names that contain special characters. Syntax Here's the basic syntax for both notations: // Dot notation object.propertyName // Bracket notation object["propertyName"] object[variableName] Basic Example: Accessing Properties Dot vs Bracket Notation ...

Read More

Setting object members in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 158 Views

In JavaScript, you can set object members (properties and methods) in several ways. This allows you to dynamically add or modify object properties after creation. Syntax // Dot notation object.propertyName = value; // Bracket notation object["propertyName"] = value; // Setting methods object.methodName = function() { // method body }; Example: Setting Object Properties Setting Object Members ...

Read More

From an array of arrays, return an array where each item is the sum of all the items in the corresponding subarray in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 360 Views

Given an array of arrays, each of which contains a set of numbers. We have to write a function that returns an array where each item is the sum of all the items in the corresponding subarray. For example, if we have nested arrays with numbers, we want to calculate the sum of each inner array and return those sums as a new array. Example Input and Expected Output If the input array is: const numbers = [ [1, 2, 3, 4], [5, 6, 7], [8, ...

Read More

How to set JavaScript object values dynamically?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 812 Views

JavaScript objects allow dynamic property assignment using bracket notation or dot notation. This enables you to set object values at runtime based on variables or user input. Syntax // Using dot notation object.propertyName = value; // Using bracket notation (dynamic) object[propertyName] = value; object['property name'] = value; Method 1: Using Dot Notation Dot notation works when you know the property name at compile time: Dynamic Object Values body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...

Read More

Modifying prototypes in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 235 Views

In JavaScript, prototypes allow you to add properties and methods to constructor functions. You can modify prototypes even after objects are created, and all existing instances will automatically inherit the changes. Understanding Prototypes Every function in JavaScript has a prototype property. When you create objects using a constructor function, they inherit methods and properties from the constructor's prototype. Example: Modifying Prototypes Modifying Prototypes body { ...

Read More

Conditional statements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

Conditional statements allow you to execute different blocks of code based on specific conditions. JavaScript provides three main types of conditional statements to control program flow. Types of Conditional Statements if statement − Executes code only if a specific condition is true. if...else statement − Checks one condition and executes different code blocks for true and false cases. if...else if...else statement − Handles multiple conditions by checking them sequentially. Syntax // if statement if (condition) { // code to execute ...

Read More

Indexed collections in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 4K+ Views

JavaScript provides indexed collections that allow you to store and access elements using numeric indices. Arrays are the most common indexed collection, where each element has a position (index) starting from 0. What are Indexed Collections? Indexed collections are data structures where elements are accessed using numerical indices. In JavaScript, arrays are the primary indexed collection type, allowing you to store multiple values and retrieve them using their position. Basic Array Creation and Access Indexed Collections ...

Read More
Showing 1211–1220 of 5,881 articles
« Prev 1 120 121 122 123 124 589 Next »
Advertisements