Nikitasha Shrivastava

Nikitasha Shrivastava

163 Articles Published

Articles by Nikitasha Shrivastava

Page 4 of 17

Finding the nth prime number in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 6K+ Views

In this article, we'll learn how to find the nth prime number using JavaScript. A prime number is a positive integer greater than 1 that has no divisors other than 1 and itself. Understanding the Problem We need to create a JavaScript function that takes an input n and returns the nth prime number. For example, if n=5, we should return 11 (the 5th prime number in the sequence: 2, 3, 5, 7, 11). Algorithm Approach We'll use a two-step approach: Helper Function: Create an isPrime() function to check if a number is prime ...

Read More

How to get almost increasing sequence of integers in JavaScript ?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 444 Views

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 More

How to shift each letter in the given string N places down in the alphabet in JavaScript?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 2K+ Views

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 More

Sorting strings with decimal points in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 1K+ Views

In JavaScript, sorting strings with decimal points requires converting them to numbers first, since string comparison would treat "10.0" as less than "2.0". This article demonstrates how to sort decimal string arrays properly. Understanding the Problem When sorting strings containing decimal numbers, JavaScript's default string comparison doesn't work numerically. For example, ['3.3', '4.4', '2.3', '1.2'] should become ['1.2', '2.3', '3.3', '4.4'], but string sorting would give incorrect results. The Solution Approach To solve this problem, we need to: Convert decimal strings to numbers using parseFloat() Sort the numbers numerically Convert back to strings if needed ...

Read More

Maximum sum of n consecutive elements of array in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 1K+ Views

In JavaScript, finding the maximum sum of n consecutive elements in an array is a classic sliding window problem. This technique efficiently calculates the maximum sum by maintaining a window of fixed size and sliding it across the array. Understanding the Problem The goal is to find a continuous subarray of length n within the given array that has the highest possible sum. For example, given array [1, 2, 4, 7, 3, 5] and n = 3, the consecutive elements [4, 7, 3] have the maximum sum ...

Read More

Recursive loop that prints an inverted count in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 476 Views

In JavaScript, we can create a recursive function to print numbers in descending order from a given number down to zero. This demonstrates how recursion can solve problems by breaking them into smaller subproblems. What is Recursion? Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem. Each recursive call works on a reduced version of the original problem until reaching a base case that stops the recursion. Classic examples include calculating factorials, Fibonacci sequences, and tree traversals. Problem Understanding Our goal is to create a function that ...

Read More

JavaScript - Find if string is a palindrome (Check for punctuation)

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 964 Views

In JavaScript, checking if a string is a palindrome while handling punctuation requires cleaning the string first. A palindrome reads the same forwards and backwards, ignoring spaces, punctuation, and case. What is a Palindrome? A palindrome is not limited to single words like "radar" or "level"; it can include phrases or sequences like "Madam, in Eden, I'm Adam." We need to ignore punctuation, spaces, and case differences to ensure accurate palindrome checks. Using String and Array Methods The first approach uses JavaScript's built-in string and array methods to normalize and reverse the string: ...

Read More

JavaScript - How to create nested unordered list based on nesting of array?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 3K+ Views

In this tutorial, we'll learn how to create nested unordered lists in HTML using JavaScript. We'll use recursion to traverse nested arrays and generate the corresponding HTML structure dynamically. Understanding the Problem We need to convert a nested array structure into an HTML unordered list where: String elements become list items Array elements become nested sublists The structure preserves the original nesting levels For example: Coffee ...

Read More

Getting century from year in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 2K+ Views

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 More

How to write the factorial function with reduce and range in JavaScript?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 1K+ Views

In this tutorial, we'll learn how to calculate factorials using JavaScript's reduce() method combined with array generation techniques. This approach demonstrates functional programming concepts while solving a common mathematical problem. Understanding reduce() and range Functions The reduce() function processes an array and reduces it to a single value by applying a function to each element. It takes an accumulator (which stores the result) and the current array value as parameters. const nums = [1, 2, 3, 4, 5]; const sum = nums.reduce((acc, val) => acc + val, 0); console.log(sum); 15 For ...

Read More
Showing 31–40 of 163 articles
« Prev 1 2 3 4 5 6 17 Next »
Advertisements