Object Oriented Programming Articles

Page 157 of 589

Absolute sum of array elements - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 882 Views

We are required to write a JavaScript function that takes in an array with both positive and negative numbers and returns the absolute sum of all the elements of the array. We are required to do this without taking help of any inbuilt library function. For example: If the array is − const arr = [1, -5, -34, -5, 2, 5, 6]; Then the output should be − 58 Understanding Absolute Sum The absolute sum means we convert all negative numbers to positive and then add all elements. For ...

Read More

Replace a value if null or undefined in JavaScript?

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

In JavaScript, you can replace null or undefined values using the logical OR operator (||) or the nullish coalescing operator (??). Syntax // Using logical OR operator var result = value || defaultValue; // Using nullish coalescing operator (ES2020+) var result = value ?? defaultValue; Using Logical OR Operator (||) The || operator returns the first truthy value or the last value if all are falsy: var value = null; console.log("The original value:", value); var actualValue = value || "This is the Correct Value"; console.log("The replaced value:", actualValue); // ...

Read More

Change every letter to next letter - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 799 Views

We are required to write a JavaScript function that takes in a string and changes every letter of the string from the English alphabets to its succeeding element. For example: If the string is − const str = 'how are you'; Then the output should be − const output = 'ipx bsf zpv'; How It Works The algorithm processes each character by checking if it's a letter using ASCII codes. For letters 'a-y' and 'A-Y', it moves to the next letter. For 'z' and 'Z', it wraps around to 'a' and 'A' respectively. Non-alphabetic ...

Read More

Is it possible to select text boxes with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 793 Views

Yes, it's possible to select text boxes with JavaScript using the select() method. This method highlights all text within an input field, making it ready for user interaction or replacement. The select() Method The select() method programmatically selects all text content in an input field. It's commonly used to improve user experience by pre-selecting text for easy editing or copying. Basic Syntax element.select(); Example: Selecting Text on Button Click Select Text Box Example ...

Read More

Repeating letter string - JavaScript

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

We are required to write a JavaScript function that takes in a string and a number, say n, and the function should return a new string in which all the letters of the original string are repeated n times. For example: If the string is − const str = 'how are you' And the number n is 2 Then the output should be − const output = 'hhooww aarree yyoouu' Example Following is the code − const str = 'how are you'; const repeatNTimes ...

Read More

JavaScript (+) sign concatenates instead of giving sum?

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

The + sign concatenates strings instead of adding numbers when JavaScript treats the values as strings. This commonly happens with form inputs, which always return string values even when they contain numbers. The Problem: String Concatenation vs Addition When you use the + operator with strings, JavaScript concatenates them instead of performing mathematical addition: String Concatenation Problem // These are strings, not numbers var a = ...

Read More

Finding missing letter in a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 760 Views

We have a string of length m that contains first m letters of the English alphabets, but somehow, one element went missing from the string. So, now the string contains, m-1 letters We are required to write a function that takes in one such string and returns the missing element from the string. Example Following is the code − const str = "acdghfbekj"; const missingCharacter = str => { // to make the function more consistent const s = str.toLowerCase(); ...

Read More

JavaScript Update specific index in a boolean matrix?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 257 Views

To update a specific index in a boolean matrix in JavaScript, you can directly access the element using array indexing and assign a new boolean value. Let's explore different approaches to create and modify boolean matrices. Creating a Boolean Matrix First, let's create a boolean matrix using the fill() method: const array = Array(4); var fillWithTrueValue = array.fill(true); const matrixWithOnlyBooleanTrue = Array(4).fill(fillWithTrueValue); console.log("Original matrix:"); console.log(matrixWithOnlyBooleanTrue); Original matrix: [ [ true, true, true, true ], [ true, true, true, true ], [ true, true, true, true ], ...

Read More

Odd even sort in an array - JavaScript

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

We are required to write a JavaScript function that takes in an array of numbers and sorts the array such that first all the even numbers appear in ascending order and then all the odd numbers appear in ascending order. For example: If the input array is − const arr = [2, 5, 2, 6, 7, 1, 8, 9]; Then the output should be − const output = [2, 2, 6, 8, 1, 5, 7, 9]; Approach The solution uses a custom comparator function that: Places even numbers ...

Read More

How do I subtract one week from this date in JavaScript?

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

To subtract one week (7 days) from a date in JavaScript, use the setDate() method combined with getDate() to modify the date object directly. Syntax var newDate = new Date(currentDate.setDate(currentDate.getDate() - 7)); Method 1: Modifying Current Date First, get the current date, then subtract 7 days using setDate(): var currentDate = new Date(); console.log("The current Date = " + currentDate); var before7Daysdate = new Date(currentDate.setDate(currentDate.getDate() - 7)); console.log("The One week ago date = " + before7Daysdate); The current Date = Tue Jul 14 2020 19:12:43 GMT+0530 (India Standard ...

Read More
Showing 1561–1570 of 5,881 articles
« Prev 1 155 156 157 158 159 589 Next »
Advertisements