Object Oriented Programming Articles

Page 156 of 589

Checking Oddish and Evenish numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 309 Views

A number is Oddish if the sum of all of its digits is odd, and a number is Evenish if the sum of all of its digits is even. We need to write a function that determines whether a number is Oddish or Evenish. The function should return true for Oddish values and false for Evenish values. How It Works The algorithm extracts each digit from the number, adds them up, and checks if the sum is odd or even: Extract digits using modulo (num % 10) and integer division (Math.floor(num / 10)) Sum all ...

Read More

How do I run two or more functions when using 'onclick' JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 642 Views

When handling click events in JavaScript, you often need to execute multiple functions. There are several ways to run two or more functions when using the onclick event. Method 1: Using a Wrapper Function Create a single function that calls multiple functions inside it: Multiple Functions onClick Call Functions function callTwoFunctions() { ...

Read More

Neutralisation of strings - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 285 Views

In JavaScript, string neutralisation involves evaluating a string containing only '+' and '-' characters to determine the overall sign. The concept is based on mathematical sign multiplication: like signs produce '+', unlike signs produce '-'. How Neutralisation Works The neutralisation follows these rules: ++ results in + (positive × positive = positive) -- results in + (negative × negative = positive) +- or -+ results in - (positive × negative = negative) Example String Let's work with the following string: const str = '+++-+-++---+-+--+-'; Implementation Here's how to implement ...

Read More

Detect the ENTER key in a text input field with JavaScript

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

Detecting the ENTER key in a text input field is a common requirement in web development. You can accomplish this using JavaScript event listeners and checking for the specific key code or key value. HTML Setup First, let's create a simple text input field: Method 1: Using keyCode (Legacy) The traditional approach uses keyCode property with value 13 for the ENTER key: ENTER Key Detection ...

Read More

Reverse only the odd length words - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 583 Views

We are required to write a JavaScript function that takes in a string and reverses the words in the string that have an odd number of characters in them. Any substring in the string qualifies to be a word, if either it is encapsulated by two spaces on either ends or present at the end or beginning and followed or preceded by a space. Let's say the following is our string: const str = 'hello beautiful people'; The odd length words are: hello (5 characters - odd) beautiful (9 characters - odd) ...

Read More

JavaScript recursive loop to sum all integers from nested array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 481 Views

JavaScript recursive functions can process nested arrays by calling themselves repeatedly to handle arrays within arrays. This technique is essential for working with complex data structures of unknown depth. Example: Basic Recursive Sum function sumOfTotalArray(numberArray) { var total = 0; for (var index = 0; index < numberArray.length; index++) { if (numberArray[index] instanceof Array) { total = total + sumOfTotalArray(numberArray[index]); } ...

Read More

Return index of first repeating character in a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 290 Views

We are required to write a JavaScript function that takes in a string and returns the index of first character that appears twice in the string. If there is no such character then we should return -1. Following is our string: const str = 'Hello world, how are you'; Example Following is the code: const str = 'Hello world, how are you'; const firstRepeating = str => { const map = new Map(); for(let i = 0; i < str.length; i++){ ...

Read More

JavaScript filter an array of strings, matching case insensitive substring?

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

To filter an array of strings with case-insensitive matching, use JavaScript's filter() method combined with toLowerCase() and indexOf(). Setting Up the Data Let's start with an array of student objects containing names with different cases: let studentDetails = [ {studentName: "John Smith"}, {studentName: "john smith"}, {studentName: "Carol Taylor"}, {studentName: "JOHN TAYLOR"}, {studentName: "alice johnson"} ]; console.log("Original array:", studentDetails); Original array: [ { studentName: 'John Smith' }, { studentName: ...

Read More

Valid triangle edges - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 524 Views

In geometry, three lines can form a triangle only if they satisfy the triangle inequality theorem: the sum of any two sides must be greater than the third side. This fundamental rule ensures that the three sides can actually connect to form a closed triangle. For example, if three lines have lengths 4, 9, and 3, they cannot form a triangle because 4 + 3 = 7, which is less than 9. At least one side would be too long to connect with the other two. Triangle Inequality Theorem For sides with lengths a, b, and c ...

Read More

Check whether Enter key is pressed or not and display the result in console with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 383 Views

In JavaScript, you can detect when the Enter key is pressed using the onkeypress event handler. This is useful for form submissions, search functionality, or triggering actions when users press Enter. Key Code for Enter Key The Enter key has a key code of 13. We can check this using the keyCode property of the event object. Basic Implementation First, create an input field with an onkeypress event handler: Then, create a function to detect the Enter key: function checkEnterKey(event) { if (event.keyCode == 13) ...

Read More
Showing 1551–1560 of 5,881 articles
« Prev 1 154 155 156 157 158 589 Next »
Advertisements