Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Object Oriented Programming Articles
Page 168 of 589
Writing a For Loop to Evaluate a Factorial - JavaScript
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 MoreGetting HTML form values and display on console in JavaScript?
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 MoreGetting equal or greater than number from the list of numbers in JavaScript
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 MorePrint JSON nested object in JavaScript?
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 MoreSum of all prime numbers in an array - JavaScript
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 MoreFilter null from an array in JavaScript?
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 MoreFinding reversed index of elements in arrays - JavaScript
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 MoreDisplay a message on console while focusing on input type in JavaScript?
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 MoreHow to access nested JSON property based on another property's value in JavaScript?
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 MoreCheck whether a string ends with some other string - JavaScript
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