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 164 of 534
Finding nth element of the Padovan sequence using JavaScript
Padovan Sequence The Padovan sequence is a sequence of integers P(n) defined by the initial values: P(0) = P(1) = P(2) = 1 and the recurrence relation: P(n) = P(n-2) + P(n-3) The first few values of P(n) are: 1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37, 49, 65, 86, 114, 151, 200, 265, ... Problem We need to write a JavaScript function that takes in a number n and returns the nth term of the Padovan sequence. Using Iterative Approach The most efficient approach uses iteration to calculate the sequence step by step: const padovan = (num = 1) => { // Handle base cases if (num
Read MoreFinding the k-prime numbers with a specific distance in a range in JavaScript
K-Prime Numbers A natural number is called k-prime if it has exactly k prime factors, counted with multiplicity. For example, 4 is a 2-prime number because 4 = 2 × 2, and both instances of 2 are counted separately. Similarly, 8 is a 3-prime number because 8 = 2 × 2 × 2, giving us three prime factors. Problem We need to write a JavaScript function that takes a number k, a distance, and a range. The function should return an array of pairs containing k-prime numbers within the range where the distance between them equals ...
Read MoreConverting an unsigned 32 bit decimal to corresponding ipv4 address in JavaScript
Converting a 32-bit unsigned integer to an IPv4 address involves extracting the four bytes that represent each octet of the IP address. Each IPv4 address consists of four octets (0-255), which can be extracted using bitwise operations. Understanding the Problem Consider the IPv4 address: 128.32.10.1 When converted to binary, each octet becomes an 8-bit binary number: 10000000.00100000.00001010.00000001 Combining these four 8-bit segments creates a 32-bit binary number, which equals the decimal value: 2149583361 Our task is to reverse this process: convert the 32-bit unsigned integer back ...
Read MoreCounting the number of IP addresses present between two IP addresses in JavaScript
We are required to write a JavaScript function that takes in two IPv4 addresses, and returns the number of addresses between them (including the first one, excluding the last one). This can be done by converting them to decimal and finding their absolute difference. How IP Address Conversion Works An IPv4 address consists of 4 octets (0-255). To convert to decimal, we use the formula: IP: a.b.c.d converts to: Decimal = a×256³ + b×256² + c×256¹ + d×256⁰ Example: 20.0.0.10 = 20×16777216 + 0×65536 + 0×256 + 10×1 ...
Read MoreGenerating the sequence of first n look and say numbers in JavaScript
Problem In mathematics, the look-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, 312211, ... To generate a member of the sequence from the previous member, we read off the digits of the previous member, counting the number of digits in groups of the same digit. For instance, the next number to 1211 is: 111221 Because if we read the digits of 1211 aloud it will be: One one, one two, two one which gives us 111221 We are required to write a JavaScript ...
Read MoreRectifying the time string in JavaScript
In JavaScript, we often need to rectify invalid time strings where the minutes or seconds exceed their maximum values (59). This function converts an invalid time format like "08:11:71" into a valid one by carrying over excess values to the next time unit. Problem We need to write a JavaScript function that takes in a time string in "HH:MM:SS" format. However, many time strings are broken, meaning the MM part may exceed 60, and SS part may exceed 60 as well. Our function should make required changes and return the rectified string. For instance: "08:11:71" ...
Read MoreWriting a custom URL shortener function in JavaScript
URL shorteners convert long URLs into shorter, more manageable links. This tutorial shows how to build a simple URL shortener with JavaScript functions for encoding and decoding URLs. Problem Statement We need to create two JavaScript functions: First function takes a long URL and returns a corresponding short URL Second function takes the short URL and returns the original long URL How It Works Our approach uses an object to store mappings between short and long URLs. The short URL is generated by extracting letters from the original URL and taking the last ...
Read MoreCreating a Projectile class to calculate height horizontal distance and landing in JavaScript
We need to create a JavaScript class called Projectile that simulates projectile motion physics. This class calculates horizontal distance traveled by a projectile given initial conditions. Problem Statement Create a Projectile class that takes three parameters during initialization: Starting height (h0): 0 ≤ h0 < 200 Starting velocity (v0): 0 < v0 < 200 Launch angle (a): 0° < a < 90°, measured in degrees The class should include a horiz method that calculates horizontal distance traveled at time t. Physics Formula The ...
Read MoreRemoving n characters from a string in alphabetical order in JavaScript
We need to write a JavaScript function that removes a specified number of characters from a string in alphabetical order. This means removing all 'a' characters first, then 'b', then 'c', and so on until we've removed the desired count. Problem Statement Given a lowercase alphabet string and a number, remove characters from the string in alphabetical order (starting with 'a', then 'b', 'c', etc.) until the specified count is reached. Example Let's implement a function that removes characters alphabetically: const str = 'abascus'; const num = 4; const removeAlphabetically = (str = ...
Read MoreCreate palindrome by changing each character to neighboring character in JavaScript
We need to write a JavaScript function that determines if we can create a palindrome by changing each character to its neighboring character in the alphabet. Each character can only move one position forward or backward (except 'a' can only go to 'b' and 'z' can only go to 'y'). Problem Requirements The function must handle these constraints: Each character MUST be changed either to the one before or the one after in the alphabet "a" can only be changed to "b" and "z" to "y" Return true if at ...
Read More