Object Oriented Programming Articles

Page 168 of 589

Writing a For Loop to Evaluate a Factorial - JavaScript

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

We are required to write a simple JavaScript function that takes in a Number, say n and computes its factorial using a for loop and returns the factorial. For example: factorial(5) = 120, factorial(6) = 720 The approach is to maintain a count and a result variable, keep multiplying the count into result, simultaneously decreasing the count by 1, until it reaches 1. Then finally we return the result. Syntax function factorial(n) { let result = 1; for (let i = n; i > ...

Read More

Getting HTML form values and display on console in JavaScript?

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

In JavaScript, you can retrieve HTML form values using the value property of form elements. This is essential for processing user input and form data. Basic Syntax To get a form element's value, use: document.getElementById("elementId").value Example: Getting Input Value Here's how to get a text input value and display it in the console: Get Form Values ...

Read More

Getting equal or greater than number from the list of numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 844 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return an array of all the elements from the input array that are greater than or equal to the number taken as the second argument. Example Following is the code − const arr = [56, 34, 2, 7, 76, 4, 45, 3, 3, 34, 23, 2, 56, 5]; const threshold = 40; const findGreater = (arr, num) => { const res = ...

Read More

Print JSON nested object in JavaScript?

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

To print JSON nested objects in JavaScript, you can use various approaches depending on the structure of your data. When dealing with nested JSON strings within objects, you'll need to parse them first using JSON.parse(). Example: Parsing Nested JSON Strings Here's how to handle objects containing JSON strings as properties: var details = [ { "studentId": 101, "studentName": "John", "countryName": "US", ...

Read More

Sum of all prime numbers 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 returns the sum of all the prime numbers present in the array. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, 11 are prime numbers. Let's say the following is our array: const arr = [43, 6, 6, 5, 54, 81, 71, 56, 8, 877, 4, 4]; The function should sum the prime numbers: 43 + 5 + 71 ...

Read More

Filter null from an array in JavaScript?

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

To filter null values from an array in JavaScript, use the filter() method. This method creates a new array containing only elements that pass a specified condition. Syntax array.filter(callback) Method 1: Using Boolean Constructor The simplest approach is to pass Boolean as the filter callback, which removes all falsy values including null, undefined, and empty strings. var names = [null, "John", null, "David", "", "Mike", null, undefined, "Bob", "Adam", null, null]; console.log("Before filtering:"); console.log(names); var filteredNames = names.filter(Boolean); console.log("After filtering null/falsy values:"); console.log(filteredNames); Before filtering: [ ...

Read More

Finding reversed index of elements in arrays - JavaScript

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

We need to create a JavaScript function that finds the reversed index of an element in an array without actually reversing it. This function calculates what position an element would occupy if the array were reversed. If the element is not present in the array, the function returns -1. Otherwise, it returns the index position the element would have in a reversed array. Understanding Reversed Index The reversed index is calculated using the formula: length - originalIndex - 1 For example, in array [45, 74, 34, 32, 23, 65]: Element 23 is at index 4 ...

Read More

Display a message on console while focusing on input type in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 315 Views

In JavaScript, you can display console messages when focusing on input elements using the focus and blur event listeners. This is useful for debugging form interactions and tracking user behavior. Basic Focus Event Example The focus event triggers when an input element receives focus, while blur triggers when it loses focus: Focus Console Messages Submit ...

Read More

How to access nested JSON property based on another property's value in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 578 Views

To access a nested JSON property based on another property's value in JavaScript, you can loop through the data and match the target property. This technique is useful when searching for specific records in JSON arrays. Example: Finding Student Marks by Subject var actualJSONData = JSON.parse(studentDetails()); var studentMarks = getMarksUsingSubjectName(actualJSONData, "JavaScript"); console.log("The student marks = " + studentMarks); function getMarksUsingSubjectName(actualJSONData, givenSubjectName) { for (var tempObj of actualJSONData) { if (tempObj.subjectName === givenSubjectName) { ...

Read More

Check whether a string ends with some other string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 211 Views

In JavaScript, there are multiple ways to check if a string ends with another string. The most straightforward approach is using the built-in endsWith() method, though you can also implement custom solutions. Using String.endsWith() (Recommended) The endsWith() method is the standard way to check if a string ends with a specific substring: const str1 = 'this is just an example'; const str2 = 'ample'; console.log(str1.endsWith(str2)); // true console.log(str1.endsWith('temple')); // false console.log(str1.endsWith('example')); // true true false true Custom Implementation ...

Read More
Showing 1671–1680 of 5,881 articles
« Prev 1 166 167 168 169 170 589 Next »
Advertisements