Object Oriented Programming Articles

Page 153 of 589

Parse JSON in JavaScript to display a specific name/value pair?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 634 Views

To parse JSON in JavaScript, you can use the native JSON.parse() method or jQuery's parseJSON() function. To display specific name/value pairs, you can use array methods like forEach() or jQuery's $.each() function. Using Native JSON.parse() The modern approach uses the built-in JSON.parse() method: Parse JSON Example const APIData = '[{"Name":"John", "Age":21}, {"Name":"David", "Age":24}, {"Name":"Bob", ...

Read More

Diagonal product of a matrix - JavaScript

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

Suppose, we have a 2-D array representing a square matrix like this − const arr = [ [1, 3, 4, 2], [4, 5, 3, 5], [5, 2, 6, 4], [8, 2, 9, 3] ]; We are required to write a function that takes in this array and returns the product of the elements present at the principal diagonal of the matrix. Principal Diagonal Elements The principal diagonal consists of elements where the row index equals the column index (i === j). For ...

Read More

How do I display only the visible text with jQuery?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 575 Views

To display only the visible text in jQuery, use the :visible selector. This selector targets elements that are currently visible on the page (not hidden with display:none, visibility:hidden, or other hiding methods). Syntax $(selector).filter(':visible') $(selector + ':visible') $(':visible') Example Visible Text with jQuery Hidden Text Visible Text ...

Read More

How to find out what character key is pressed in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 738 Views

To find out which character key is pressed in JavaScript, you can use various methods. The modern approach uses the key property, while legacy methods relied on keyCode. Modern Approach: Using event.key The key property directly returns the character that was pressed, making it the preferred method: Key Detection - Modern Method Type in the input field below: ...

Read More

Corner digit number difference - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 170 Views

We are required to write a JavaScript function that takes in a number, constructs a new number from the first and last digit of that number and returns the difference between the original number and the number thus formed. For example: If the input is 34567 Then the corner digits number will be: 37 And the output will be: 34530 Algorithm The solution involves extracting the first and last digits, combining them to form a corner number, then calculating the difference. Example Following is the code: ...

Read More

Simplest way to detect keypresses in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 356 Views

The simplest way to detect keypresses in JavaScript is using the onkeypress event handler. However, modern JavaScript also provides addEventListener for better event handling. document.onkeypress The key press is matched with the keyCode property, which returns the Unicode character code of the key that triggered the onkeypress event. Using document.onkeypress Keypress Detection Press any key to see the detection in action! ...

Read More

Check three consecutive numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 551 Views

We are required to write a JavaScript function that takes in a Number, say n, and we are required to check whether there exist such three consecutive natural numbers (not decimal/floating point) whose sum equals to n. If there exist such numbers, our function should return them, otherwise it should return false. Following is the code − How It Works For three consecutive numbers to sum to n, we need: x + (x+1) + (x+2) = n, which simplifies to 3x + 3 = n. Therefore, n must be divisible by 3 and greater than 5 (since ...

Read More

Convert HTML table to array in JavaScript?

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

Converting an HTML table to an array in JavaScript involves extracting data from table cells and storing them in a structured format. This is commonly done using DOM manipulation methods or jQuery. HTML Table Structure Let's start with a sample HTML table: Name Age John 23 ...

Read More

Check if value is empty in JavaScript

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

In JavaScript, checking if a value is empty is essential for form validation and data processing. You can check for empty strings, null values, and undefined variables using various approaches. Basic Empty Check The most common approach is to check for empty strings and null values: Check Empty Value USERNAME: ...

Read More

How to capitalize the first letter of each word in a string using JavaScript?

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

In JavaScript, you can capitalize the first letter of each word in a string by splitting the string into individual words, capitalizing each word's first letter, and then joining them back together. Syntax string.split(' ') // Split string into array of words word.charAt(0) // Get first character word.toUpperCase() // Convert to uppercase word.substring(1) // Get ...

Read More
Showing 1521–1530 of 5,881 articles
« Prev 1 151 152 153 154 155 589 Next »
Advertisements