Object Oriented Programming Articles

Page 105 of 589

Verification if a number is Palindrome in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 711 Views

A palindrome number reads the same forwards and backwards. In JavaScript, we can check if a number is palindrome without converting it to a string by mathematically extracting and comparing digits. Palindrome numbers are those numbers which read the same from both backward and forward. For example: 121 343 12321 Algorithm Approach The algorithm works by: Finding a factor to extract the first digit Comparing first and last digits Removing both digits and repeating until all digits are checked Example const isPalindrome = (num) => { ...

Read More

Window innerWidth and innerHeight Properties in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 250 Views

The window.innerWidth and window.innerHeight properties return the width and height of the browser window's content area, excluding toolbars, scrollbars, and borders. Syntax let width = window.innerWidth; let height = window.innerHeight; Properties innerWidth - Returns the interior width of the window in pixels innerHeight - Returns the interior height of the window in pixels Both properties are read-only and return integer values Values change when the browser window is resized Example Window Dimensions ...

Read More

Highlight a text, every time page loads with JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 360 Views

To highlight text every time a page loads in JavaScript, you can use a for loop to iterate through words and wrap specific words in elements with CSS classes for styling. Approach The technique involves: Getting the text content using getElementById() Splitting the text into an array of words using split(" ") Looping through each word to find matches Wrapping matching words with tags Rejoining the array and updating the HTML content Example Text ...

Read More

How to invoke a function as a function and method?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 175 Views

In JavaScript, functions can be invoked in different ways. When called directly, it's function invocation. When called as a property of an object, it's method invocation. Function vs Method Invocation Function invocation: Calling a function directly by its name. Method invocation: Calling a function that belongs to an object using dot notation. Example Function and Method Invocation body { ...

Read More

Sum of even numbers up to using recursive function in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 757 Views

We have to write a recursive function that takes in a number n and returns the sum of all even numbers up to n. A recursive function calls itself with modified parameters until it reaches a base case. For summing even numbers, we'll start from the largest even number ≤ n and work our way down. How It Works The algorithm follows these steps: If the input number is odd, we adjust it to the nearest even number below it Add the current even number to the sum and recursively call with the next smaller ...

Read More

Block Scoping in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 360 Views

Block scope is an area between two curly braces { } which can be between loops, if conditions, or switch statements. The let and const keywords introduced in ES2015 allow us to create block-scoped variables that can only be accessed within that specific block. Understanding Block Scope Variables declared with let and const have block scope, meaning they exist only within the nearest enclosing block. This is different from var, which has function scope. Example: Block Scope with let Block Scoping Example ...

Read More

Get unique item from two different array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 221 Views

We are required to make a function that accepts an array of arrays, and returns a new array with all elements present in the original array of arrays but remove the duplicate items. For example − If the input is − const arr = [ [12, 45, 65, 76, 76, 87, 98], [54, 65, 98, 23, 78, 9, 1, 3], [87, 98, 3, 2, 123, 877, 22, 5, 23, 67] ]; Then the output should be single array of unique elements like this − ...

Read More

Explain JavaScript Error Object.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 372 Views

The Error object is thrown by the JavaScript interpreter when a script error occurs. It can also be used to create custom exceptions. Understanding Error objects helps in debugging and error handling in JavaScript applications. Error Object Properties Property Description name Sets or returns the name/type of the error message Sets or returns the error message as a string Example: Catching and Displaying Error Properties JavaScript Error Object body { ...

Read More

Pad a string using random numbers to a fixed length using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 440 Views

We need to write a function that takes a string and a target length, then pads the string with random numbers until it reaches the specified length. This is useful for creating fixed-length identifiers or codes. How It Works The function uses recursion to add random digits (0-9) to the end of the string until it reaches the target length. Each recursive call generates a new random digit using Math.random(). Implementation const padString = (str, len) => { if(str.length < len){ const random ...

Read More

How to remove elements using the splice() method in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 369 Views

The splice() method is used to remove elements from an array by specifying the starting index and the number of elements to delete. It modifies the original array and returns an array of the removed elements. Syntax array.splice(startIndex, deleteCount) Parameters startIndex: The index at which to start removing elements deleteCount: The number of elements to remove Example: Remove Single Element Splice Method Example Remove Elements using ...

Read More
Showing 1041–1050 of 5,881 articles
« Prev 1 103 104 105 106 107 589 Next »
Advertisements