Object Oriented Programming Articles

Page 165 of 589

How to set text in tag with JavaScript?

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

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 More

Prime numbers upto n - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 697 Views

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 More

Insert a word before the file name's dot extension in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 502 Views

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 More

Replacing all special characters with their ASCII value in a string - JavaScript

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

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 More

JavaScript Adding array name property to JSON object [ES5] and display in Console?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 236 Views

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 More

Inserting element at falsy index in an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 246 Views

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 More

JavaScript to check consecutive numbers in array?

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

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 More

Array of adjacent element's average - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 324 Views

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 More

How to use the map function to see if a time is in a certain time frame with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 271 Views

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 More

Build maximum array based on a 2-D array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 168 Views

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
Showing 1641–1650 of 5,881 articles
« Prev 1 163 164 165 166 167 589 Next »
Advertisements