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 165 of 589
How to set text in tag with JavaScript?
In JavaScript, you can set text in HTML tags using various methods. This tutorial shows different approaches including jQuery and vanilla JavaScript. Using jQuery .html() Method First, create a element with an ID attribute: Replace This strong tag Then use jQuery to set the text content: $(document).ready(function(){ $("#strongDemo").html("Actual value of 5+10 is 15....."); }); Complete jQuery Example Set Text in Tag ...
Read MorePrime numbers upto n - JavaScript
Let's say, we are required to write a JavaScript function that takes in a number, say n, and returns an array containing all the prime numbers up to n. For example − If the number n is 24, then the output should be − const output = [2, 3, 5, 7, 11, 13, 17, 19, 23]; Method 1: Basic Prime Check Approach This approach uses a helper function to check if each number is prime: const num = 24; const isPrime = num => { let count ...
Read MoreInsert a word before the file name's dot extension in JavaScript?
To insert a word before a file's extension, you can split the filename on the dot (.) and then reconstruct it with the new word. This technique is useful for creating versioned files, adding timestamps, or modifying filenames programmatically. Basic Example Let's start with a simple example where we insert "programming" before the ".js" extension: var actualJavaScriptFileName = "demo.js"; var addValueBetweenFileNameAndExtensions = "programming"; console.log("The actual File name = " + actualJavaScriptFileName); var [fileName, fileExtension] = actualJavaScriptFileName.split('.'); var modifiedFileName = `${fileName}-${addValueBetweenFileNameAndExtensions}.${fileExtension}`; console.log("After adding into the file name:"); console.log(modifiedFileName); The actual File ...
Read MoreReplacing all special characters with their ASCII value in a string - JavaScript
We are required to write a JavaScript function that takes in a string that might contain some special characters. The function should return a new string with all special characters replaced with their corresponding ASCII value. Understanding Special Characters Special characters are symbols that are not letters, numbers, or spaces. Examples include !, @, #, $, etc. Each character has a unique ASCII code that can be retrieved using the charCodeAt() method. Example Following is the code: const str = 'Th!s !s @ str!ng th@t cont@!ns some special characters!!'; const specialToASCII = str ...
Read MoreJavaScript Adding array name property to JSON object [ES5] and display in Console?
In JavaScript, you can add a JSON object to an array and assign it a property name for better organization. This is useful when you need to convert object data into array format while maintaining structure. Initial Object Setup Let's start with a sample customer object: var customerDetails = { "customerFirstName": "David", "customerLastName": "Miller", "customerAge": 21, "customerCountryName": "US" }; Adding Object to Array Using push() Create a new array and use the push() method to add the ...
Read MoreInserting element at falsy index in an array - JavaScript
We are required to write an Array function, let's say, pushAtFalsy() The function should take in an array and an element. It should insert the element at the first falsy index it finds in the array. If there are no empty spaces, the element should be inserted at the last of the array. We will first search for the index of empty position and then replace the value there with the value we are provided with. Understanding Falsy Values In JavaScript, falsy values include: null, undefined, false, 0, "" (empty string), and NaN. However, since 0 ...
Read MoreJavaScript to check consecutive numbers in array?
To check for consecutive numbers in an array, you can use JavaScript's reduce() method or simpler approaches. This returns true for consecutive sequences like 100, 101, 102, and false otherwise. Method 1: Using reduce() with Objects This approach works with arrays of objects containing number properties: const sequenceIsConsecutive = (obj) => Boolean(obj.reduce((output, latest) => (output ? (Number(output.number) + 1 === Number(latest.number) ? latest : false) : false))); console.log("Is Consecutive = " + sequenceIsConsecutive([ ...
Read MoreArray of adjacent element's average - JavaScript
Let's say, we have an array of numbers: const arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, 4, 2, 1]; We are required to write a function that returns an array with the average of the corresponding element and its predecessor. For the first element, as there are no predecessors, so that very element should be returned. Let's write the code for this function, we will use the Array.prototype.map() function to solve this problem: Example const arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, ...
Read MoreHow to use the map function to see if a time is in a certain time frame with JavaScript?
In JavaScript, you can use the map() function to iterate through schedule records and check if the current time falls within a specific time frame. This is useful for determining which activity or subject you should be working on at any given moment. Example Data Structure Let's start with a simple schedule containing subject names and their study times: const scheduleDetails = [ { subjectName: 'JavaScript', studyTime: '5 PM - 11 PM' }, { subjectName: 'MySQL', studyTime: '12 AM - 4 PM' } ] Using map() ...
Read MoreBuild maximum array based on a 2-D array - JavaScript
Let's say, we have an array of arrays of Numbers like below − const arr = [ [1, 16, 34, 48], [6, 66, 2, 98], [43, 8, 65, 43], [32, 98, 76, 83], [65, 89, 32, 4], ]; We are required to write a function that maps over this array of arrays and returns an array that contains the maximum (greatest) element from each subarray. So, for the above array, the output should be − [48, 98, 65, 98, 89] Using ...
Read More