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 252 of 534
Getting century from year in JavaScript
In JavaScript, determining the century from a given year is a common programming task. A century represents a period of 100 years, where the 1st century includes years 1-100, the 20th century covers 1901-2000, and so on. Understanding Century Calculation The key insight is that centuries don't align perfectly with hundreds. For example: Year 1900 belongs to the 19th century (not 20th) Year 2000 belongs to the 20th century (not 21st) Year 2001 begins the 21st century Logic and Algorithm To find the century from a year: Divide the year by 100 Round ...
Read MoreGiven an array of integers return positives, whose equivalent negatives present in it in JavaScript
In this problem, we need to find positive integers from an array that have their corresponding negative counterparts present in the same array. For example, if we have [1, -1, 2, -3, 4, -4], we should return [1, 4] since both 1 and 4 have their negatives (-1 and -4) in the array. Understanding the Problem Given an array of integers containing both positive and negative values, we need to identify positive numbers whose negative equivalents also exist in the array. For instance, in the array [7, ...
Read MoreGroup a sorted array based on the difference between current and previous elements in JavaScript
In JavaScript, grouping a sorted array based on the difference between consecutive elements is a common problem where we need to create separate groups whenever the difference exceeds a certain threshold (typically 1). This technique is useful for analyzing sequences and identifying continuous ranges in data. Understanding the Problem Given a sorted array, we need to group elements where consecutive elements have a difference of 1 or less. When the difference between current and previous elements exceeds 1, we start a new group. For example, the array [1, 2, ...
Read MoreHow to get almost increasing sequence of integers in JavaScript ?
In JavaScript, an almost increasing sequence is a sequence where most elements are in non-decreasing order, with at most one element that breaks this pattern. This article demonstrates how to generate such sequences programmatically. Understanding the Problem An almost increasing sequence allows for exactly one "violation" where an element might be smaller than its predecessor. For example, [1, 3, 2, 5, 6] is almost increasing because only one element (2) breaks the increasing pattern. Our goal is to generate such sequences randomly in JavaScript. Algorithm Approach We'll create a function that generates a mostly increasing sequence ...
Read MoreHow to reverse a string using only one variable in JavaScript
In this problem statement, our target is to print the reverse string using only one variable and implement the solution with the help of JavaScript. We can solve this problem with the help of loops in JavaScript. Understanding the Problem The given problem is stating that we have a string which we need to reverse using only one variable. In simple terms, if we have the string "Hello World", the reverse of this string will be "dlroW olleH". Logic for the Given Problem In order to reverse the given string with only one variable, we will ...
Read MoreHow to shift each letter in the given string N places down in the alphabet in JavaScript?
In the given problem statement our aim is to shift every letter in the given string N places down in the alphabet with the help of JavaScript functionalities. This is also known as the Caesar cipher technique. Understanding the Problem The problem at hand is to shift each character of the given string N places down in the alphabet using JavaScript. We need to take a string as input and update it by shifting every letter N positions down in the alphabet. For example, if the letter ...
Read MoreHow to split sentence into blocks of fixed length without breaking words in JavaScript
In JavaScript, splitting a sentence into blocks of fixed length while keeping words intact requires careful handling of word boundaries. This ensures readability by avoiding broken words across blocks. Understanding the Problem We need to split a sentence into chunks of a specified maximum length without breaking words. Each block should contain complete words only, and if adding a word would exceed the length limit, that word starts a new block. For example, with the sentence "You are reading this article on Tutorials point website" and block length 15, the output should be: ['You are ...
Read MoreisSubset of two arrays in JavaScript
In JavaScript, checking if one array is a subset of another means verifying that all elements of the second array exist in the first array. This is a common programming problem that can be solved efficiently using JavaScript's Set data structure. Understanding the Problem An array is considered a subset of another array if all its elements are present in the parent array. For example, [2, 4, 6] is a subset of [1, 2, 3, 4, 5, 6] because all elements (2, 4, 6) exist in the larger array. Using Set for Efficient Lookup The most ...
Read MoreJavaScript Total subarrays with Sum K
In this problem, we need to find the total number of continuous subarrays within an array that have a sum equal to a given value K using JavaScript. Understanding the Problem Given an array of integers and a target sum K, we need to count all possible subarrays whose elements sum up to K. A subarray is a contiguous sequence of elements within an array. For example, if we have array [1, 2, 3, 4, 5] and K = 6, the subarrays with sum 6 are [1, 2, 3] and [2, 4], giving us a count of 2. ...
Read MoreLargest difference between element with a twist in JavaScript
In this problem, we need to find the largest difference between elements with a twist using JavaScript. The twist is that we can only calculate the difference between an element and any smaller element that appeared before it in the array. Understanding the Problem Unlike finding the simple maximum difference between any two elements, this problem requires us to: Maintain the original array order (no sorting) Only consider differences where the larger element comes after the smaller one Find the maximum possible difference under these constraints ...
Read More