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 94 of 534
Finding the 1-based index of a character in alphabets using JavaScript
We are required to write a JavaScript function that takes in a lowercase English alphabet character. Our function should return the character's 1-based index in the alphabets. Problem Given a lowercase English alphabet character, we need to find its position in the alphabet where 'a' = 1, 'b' = 2, 'c' = 3, and so on. Method 1: Using String indexOf() We can create a string containing all alphabets and use indexOf() to find the position: const char = 'j'; const findCharIndex = (char = '') => { const legend ...
Read MoreConstructing an array of addition/subtractions relative to first array element in JavaScript
We are required to write a JavaScript function that takes in an array of positive integers. Our function should map this array to an array of string integers. The array should contain the number we should add/subtract to the first element to achieve the corresponding element. Problem For example, if we have: [4, 3, 6, 2] We need to calculate the difference between each element and the first element (4): 4 - 4 = 0 → "+0" 3 - 4 = -1 → "-1" 6 - 4 = 2 → "+2" 2 ...
Read MoreGenerating desired pairs within a range using JavaScript
We are required to write a JavaScript function that takes in a number n. Our function should generate an array containing the pairs of integers [a, b] that satisfy the following conditions: 0
Read MoreIs the digit divisible by the previous digit of the number in JavaScript
Problem We need to write a JavaScript function that takes a number and checks if each digit is divisible by the digit on its left. The function returns an array of booleans, where the first element is always false since there's no digit before the first one. Example Let's check the number 73312 digit by digit: const num = 73312; const divisibleByPrevious = (n = 1) => { const str = n.toString(); const arr = [false]; for(let i = 1; ...
Read MoreSorting and find sum of differences for an array using JavaScript
Problem We need to write a JavaScript function that takes an array of integers, sorts them in descending order, and then calculates the sum of differences between consecutive pairs. For example, if the array is: [6, 2, 15] After sorting in descending order: [15, 6, 2] The sum of differences would be: (15 - 6) + (6 - 2) = 9 + 4 = 13 Solution Here's the implementation that sorts the array and calculates the sum of consecutive differences: const arr = [6, 2, 15]; ...
Read MoreChanging second half of string number digits to zero using JavaScript
Problem We are required to write a JavaScript function that takes in a string number as the only argument. Our function should return the input number with the second half of digits changed to 0. In cases where the number has an odd number of digits, the middle digit onwards should be changed to 0. For example: 938473 → 938000 How It Works The algorithm works by calculating the midpoint of the string. For even-length strings, exactly half the digits are preserved. For odd-length strings, the middle digit becomes part of ...
Read MoreAccumulating array elements to form new array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a number, num, (num ≤ length of array) as the second argument. Our function should add up each contiguous subarray of length num of the array arr to form corresponding elements of new array and finally return that new array. Problem Example For example, if the input to the function is: const arr = [1, 2, 3, 4, 5, 6]; const num = 2; Then the output should be: [3, ...
Read MoreChecking for special numbers in JavaScript
In JavaScript, checking for special numbers often involves mathematical operations on digits. A palindrome digit sum check determines if the sum of a number's digits forms a palindrome. Problem We need to write a JavaScript function that takes a number and returns true if the sum of its digits is a palindrome number, false otherwise. For example, with input 781296: const num = 781296; The expected output is: true Output Explanation The digit sum of 781296 is 7+8+1+2+9+6 = 33, which reads the same forwards and backwards (palindrome). ...
Read MoreMerging nested arrays to form 1-d array in JavaScript
We are required to write a JavaScript function that takes in two nested arrays, arr1 and arr2, as the first and the second argument. Our function should create and return a third array that contains all the elements of arr1 and arr2 but flattened to single dimension For example, if the input to the function is — const arr1 = [ 1, [ 2, [ 4, 5, [ ...
Read MoreRemoving comments from array of string in JavaScript
We are required to write a JavaScript function that takes in array of strings, arr, as the first argument and an array of special characters, starters, as the second argument. The starter array contains characters that can start a comment. Our function should iterate through the array arr and remove all the comments contained in the strings. Problem Example For example, if the input to the function is: const arr = [ 'red, green !blue', 'jasmine, #pink, cyan' ]; const starters = ['!', '#']; Then the output ...
Read More