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
Javascript Articles
Page 475 of 534
Implement Onclick in JavaScript and allow web browser to go back to previous page?
JavaScript provides several methods to implement browser navigation functionality. The most common approach is using window.history.go(-1) with an onclick event to go back to the previous page. Syntax window.history.go(-1); // Go back one page window.history.back(); // Alternative method Using window.history.go(-1) The window.history.go(-1) method navigates back one page in the browser history: Go Back Example Current Page ...
Read MoreHow to check for 'undefined' or 'null' in a JavaScript array and display only non-null values?
In JavaScript, arrays can contain null or undefined values. To display only non-null and non-undefined values, you need to check each element before processing it. Sample Array with Mixed Values Let's start with an array containing valid strings, null, and undefined values: var firstName = ["John", null, "Mike", "David", "Bob", undefined]; console.log("Original array:", firstName); Original array: [ 'John', null, 'Mike', 'David', 'Bob', undefined ] Method 1: Using for Loop with != undefined This approach checks if each element is not undefined. Since null != undefined evaluates to true, this will ...
Read MoreHow do I trigger a function when the time reaches a specific time in JavaScript?
To trigger a function at a specific time in JavaScript, calculate the time difference between the current time and target time, then use setTimeout() with that delay. Basic Approach Extract the target time, calculate the milliseconds until that time, and schedule the function execution: Trigger Function at Specific Time Function will trigger at specified time Waiting for target time... ...
Read MoreHow can I check JavaScript arrays for empty strings?
In JavaScript, arrays can contain empty strings alongside other values. Here are several methods to check for and handle empty strings in arrays. Method 1: Using a Loop to Find Empty Strings The most straightforward approach is to iterate through the array and check each element: var studentDetails = new Array(); studentDetails[0] = "John"; studentDetails[1] = ""; studentDetails[2] = "Smith"; studentDetails[3] = ""; studentDetails[4] = "UK"; function arrayHasEmptyStrings(studentDetails) { for (var index = 0; index < studentDetails.length; index++) { if (studentDetails[index] == "") ...
Read MoreRotating an array - JavaScript
In JavaScript, rotating an array means moving elements from one position to another by a specified number of steps. This operation is useful for circular data structures and algorithmic problems. For example, if we have an array [12, 6, 43, 5, 7, 2, 5] and rotate it by 3 positions to the left, the result would be [5, 7, 2, 5, 12, 6, 43]. Method 1: Using Array Prototype Extension We can extend the Array prototype to add a rotation method: // Helper function to rotate by one position const rotateByOne = arr => { ...
Read MoreCount of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
We are required to write a JavaScript function that takes in three numbers A, B and N, and finds the total number of N-digit numbers whose sum of digits at even positions and odd positions are divisible by A and B respectively. Understanding the Problem For an N-digit number, we need to: Calculate sum of digits at even positions (0-indexed) Calculate sum of digits at odd positions (0-indexed) Check if even position sum is divisible by A Check if odd position sum is divisible by B Helper Function: Calculate Position-wise Sums First, let's ...
Read MoreFinding how many time a specific letter is appearing in the sentence using for loop, break, and continue - JavaScript
We are required to write a JavaScript function that finds how many times a specific letter is appearing in the sentence using for loop, break, and continue statements. Using continue Statement The continue statement skips the current iteration and moves to the next one. Here's how we can use it to count character occurrences: const string = 'This is just an example string for the program'; const countAppearances = (str, char) => { let count = 0; for(let i = 0; i < str.length; i++){ ...
Read MoreCount groups of negatives numbers in JavaScript
We have an array of numbers like this: const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]; Let's say, we are required to write a JavaScript function that counts the consecutive groups of negative numbers in the array. Like here we have consecutive negatives from index 0 to 2 which forms one group and then from 4 to 8 forms the second group. So, for this array, the function should return 2. Understanding the Problem A group of consecutive negative numbers is defined as: One ...
Read MoreCompare array of objects - JavaScript
We have two arrays of objects like these: const blocks = [ { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, ] const containers = [ { block: { id: 1 } }, { block: { id: 2 } }, { block: { id: 3 } }, ] We are required to write a function that checks each object of blocks array with the block key of each ...
Read MoreDelete duplicate elements based on first letter – JavaScript
We need to write a JavaScript function that removes duplicate strings based on their first letter, keeping only the first occurrence of each starting letter. For example, if we have an array like: const arr = ['Apple', 'Jack', 'Army', 'Car', 'Jason']; We should keep only one string for each starting letter. Since 'Apple' and 'Army' both start with 'A', we keep only 'Apple' (first occurrence). Similarly, 'Jack' and 'Jason' both start with 'J', so we keep only 'Jack'. Problem with Direct Array Modification The original approach has a bug - modifying an array ...
Read More