Add a method to a JavaScript object constructor?

Vivek Verma
Updated on 24-Jul-2025 11:26:03

3K+ Views

In JavaScript, adding a method (function) to a constructor is different from adding a method to a regular object. To add a method to a constructor, we need to define it inside the constructor or in its prototype (using which JavaScript objects inherit features from one another). We can create an object in the following ways in JavaScript: Using the function Constructor (older way): function person(){ } const p = new person(); Using ES6 Class Syntax (i.e., modern way): class person{ constructor(){} } const p = new person(); Let's see the various ways to add ... Read More

Accessing array out of bounds in C/C++

Ravi Ranjan
Updated on 23-Jul-2025 18:57:52

2K+ Views

An array in C/C++ is a fixed-size sequential collection of elements of the same data type where all the elements are stored in the contiguous memory allocation. If an array is accessed out of bounds then an undefined behavior will occur in C/C++, unlike Java where an exception such as java.lang.ArrayIndexOutOfBoundsException will occur. Accessing Out of Bound Memory Accessing out-of-bounds memory in an array means we are trying to access the array index outside its valid range size (i.e., index = array size). It returns any garbage value in the output. Example In ... Read More

Maximum triplet sum in array in C++

Ravi Ranjan
Updated on 23-Jul-2025 18:52:56

345 Views

In this article, we are given an array of integers. Our task is to write a program to calculate the maximum triplet sum in the given array, i.e., find the set of three elements whose sum is maximum. Input Output Scenario Consider the following input and output scenario where we have calculated the sum of each triplet and then returned the maximum sum of the triplet in the array: Input: arr = {4, 6, 1, 2} Output: Maximum triplet sum: 12 Here is an explanation of the above example: All ... Read More

Check if a number is Quartan Prime or not in C++

Akansha Kumari
Updated on 23-Jul-2025 18:48:18

394 Views

The Quartan primes are prime numbers that can be represented as x^4 + y^4. Where x, y > 0. Some Quartan prime numbers are {2, 17, 97, …}.In this article, we will learn how to check whether a number is Quartan Prime or not in C++. Consider the following input and output scenarios to understand the concept better: Scenario 1 Input: 17 Output: The number is Quartan Prime Explanation Here, we can represent the given number in the form of x^4 + y^4; (1)^4 + (2)^4 => 1 + 16 = 17. Scenario 2 Input: 12 Output: The number ... Read More

Check if a given matrix is Hankel or not in C++

Akansha Kumari
Updated on 23-Jul-2025 18:44:13

228 Views

The Hankel matrix is a square matrix, in which each ascending skew-diagonal or anti-diagonal (from top-right to bottom-left) elements are constant. For example a matrix name M with size 5x5 is given below − 1 2 3 4 ... Read More

Add two unsigned numbers using bits in C++.

Nishu Kumari
Updated on 23-Jul-2025 18:12:49

750 Views

In this problem, we are given two unsigned numbers, and we need to add them using bits in C++. Bits are binary digits that can be either 0 or 1. Unsigned numbers are positive numbers represented by these bits. To add two unsigned numbers, we add their bits one by one using binary addition rules. Binary addition works like decimal addition, but with simpler rules: 1 + 0 = 1 0 + 1 = 1 0 + 0 = 0 1 + 1 ... Read More

Program to add two binary strings in C++

Nishu Kumari
Updated on 23-Jul-2025 18:01:13

12K+ Views

In this problem, we are given two binary strings, and we need to find their sum and return the result as a binary string. A binary string is a string that contains only the characters '0' and '1', where 0 and 1 are binary numbers. While adding two binary numbers, we follow the binary addition rules given below - 1 + 0 = 1 0 + 1 = 1 0 + 0 = 0 1 + 1 = 0 with a carry of 1 ... Read More

Advantages of vector over array in C++

Nishu Kumari
Updated on 23-Jul-2025 17:42:11

425 Views

In C++, both arrays and vectors are used to store elements, but the main difference is that arrays have a fixed size and cannot be changed once initialized. Whereas, vectors are dynamic, i., e we can change their size during runtime. In this article, we'll look at the advantages of using vectors over arrays in C++. Here's how we declare an array and a vector in C++: // Declaring an array int arr[5]; // Fixed-size array of 5 integers // Declaring a vector #include std::vector vec; // Dynamic vector of integers Advantages of ... Read More

What are type specifiers in C++?

Akansha Kumari
Updated on 23-Jul-2025 15:46:50

3K+ Views

In a statically typed language such as C++, type specifiers are keywords that are used to define the type of data that given variables will hold. There are two types of type specifiers: built-in and user-defined type specifiers. Built-in Type SpecifiersThe built-in type specifiers are the basic and predefined data types provided by C++, such as int, float, char, signed, unsigned, short, long, etc. int myNumber = 42; In this given statement, the "int" is a type specifier, which states that the variable "myNumber" can only store integer values or numeric data types. There exist a lot of built-in type specifiers in C++ ... Read More

4Sum in C++

Akansha Kumari
Updated on 23-Jul-2025 15:44:35

612 Views

4SUM ProblemThe 4Sum is one of the problem variations in which we need to find the number of quadruplets present in the array such that their sum is equal to the given target. We are given an array of n integers called nums and a target value, and we have to find the number of index quadruplets (i, j, k, l) where i, j, k, and l are all indices in the range 0 to n - 1, such that they satisfy the condition: nums[i] + nums[j] + nums[k] + nums[l] = target Scenario 1 Inputs: arr= [-1, 0, 1, 2, ... Read More

Advertisements