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 89 of 534
Replacing vowels with their 1-based index in a string in JavaScript
We are required to write a JavaScript function that takes in a string and replaces all occurrences of the vowels in the string with their index in the string (1-based). It means if the second letter of the string is a vowel, it should be replaced by 2. Example Following is the code − const str = 'cancotainsomevowels'; const replaceVowels = (str = '') => { const vowels = 'aeiou'; let res = ''; for(let i = 0; i < str.length; i++){ ...
Read MoreFinding a number, when multiplied with input number yields input number in JavaScript
We need to write a JavaScript function that takes a positive integer n and a positive integer p, then finds a value k such that the sum of digits raised to successive powers equals k * n. Problem Statement Given a number n with digits a, b, c, d... and a power p, we want to find integer k where: (a^p + b^(p+1) + c^(p+2) + d^(p+3) + ...) = n * k If such k exists, return k; otherwise return -1. Example Walkthrough For number 695 and p = 2: 6^2 + ...
Read MoreCounting number of 9s encountered while counting up to n in JavaScript
We need to write a JavaScript function that counts how many times the digit "9" appears when counting from 0 to a given number n. For example, counting from 0 to 100 includes numbers like 9, 19, 29, 90, 91, 99, etc., where "9" appears multiple times. Problem We are required to write a JavaScript function that takes in a number n. Our function should count and return the number of times we will have to use the digit 9 while counting from 0 to n. Example Following is the code − const num ...
Read MoreChecking if a string contains all unique characters using JavaScript
Problem We are required to write a JavaScript function that takes in a string and returns true if all the characters in the string appear only once and false otherwise. Method 1: Using indexOf() and lastIndexOf() This approach compares the first and last occurrence of each character. If they differ, the character appears multiple times. const str = 'thisconaluqe'; const allUnique = (str = '') => { for(let i = 0; i < str.length; i++){ const el = str[i]; ...
Read MoreIs the reversed number a prime number in JavaScript
We are required to write a JavaScript function that takes in a number and returns true if the reverse of that number is a prime number, false otherwise. Problem Understanding To solve this problem, we need two helper functions: A function to reverse the digits of a number A function to check if a number is prime Solution Implementation Here's the complete solution with helper functions: const num = 13; const findReverse = (num) => { return +num .toString() .split('') .reverse() .join(''); }; const isPrime = (num) => { if (num
Read MoreMerging two sorted arrays into one sorted array using JavaScript
We are required to write a JavaScript function that takes in two sorted arrays of numbers. Our function should merge all the elements of both arrays into a new array and return that new array sorted in the same order. Two-Pointer Approach The most efficient approach uses two pointers to traverse both arrays simultaneously, comparing elements and adding the smaller one to the result array. const arr1 = [1, 3, 4, 5, 6, 8]; const arr2 = [4, 6, 8, 9, 11]; const mergeSortedArrays = (arr1 = [], arr2 = []) => { ...
Read MoreVolume difference of cuboids in JavaScript
We are required to write a JavaScript function that takes in two arrays, specifying the lengths, widths, and heights of two cuboids. Our function should calculate the volume of both cuboids and return their absolute difference. Formula The volume of a cuboid is calculated as: Volume = length × width × height Example Following is the code: const h1 = 10; const w1 = 12; const l1 = 15; const h2 = 12; const w2 = 15; const l2 = 9; const findVolumeDifference = (l1, w1, h1, l2, w2, ...
Read MoreSum all perfect cube values upto n using JavaScript
Problem We are required to write a JavaScript function that takes in a number n and returns the sum of all perfect cube numbers smaller than or equal to n. Understanding Perfect Cubes Perfect cubes are numbers that can be expressed as n³ where n is an integer. For example: 1³ = 1, 2³ = 8, 3³ = 27, 4³ = 64, etc. Example Following is the code − const num = 23546; const sumPerfectCubes = (num = 1) => { let i = 1; let sum = 0; while(i * i * i
Read MoreSplitting number to contain continuous odd or even number using JavaScript
Problem We need to write a JavaScript function that takes a positive number and splits it into continuous segments of odd or even digits. The function should create a new segment whenever it encounters a digit with different parity (odd becomes even or even becomes odd). Example For the number 124579: 1 (odd) - starts first segment 2, 4 (even) - starts second segment 5, 7, 9 (odd) - starts third segment Solution Following is the code: const num = 124579; const splitDifferent = (num = 1) => { const ...
Read MoreConstructing a sentence based on array of words and punctuations using JavaScript
We need to write a JavaScript function that takes an array of words and punctuations and constructs a proper sentence following specific spacing and punctuation rules. Problem Statement Our function should join array elements to construct a sentence based on the following rules: There must always be a space between words There must not be a space between a comma and the word on the left There must always be one and only one period at the end of a sentence Solution Here's how ...
Read More