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
Web Development Articles
Page 194 of 801
Checking for vowels in array of numbers using JavaScript
We are required to write a JavaScript function that takes in an array of numbers. If in that array there exists any number which is the char code of any vowel in ASCII, we should switch that number to the corresponding vowel and return the new array. Understanding ASCII Character Codes In ASCII, vowels have specific character codes: 'a' = 97 'e' = 101 'i' = 105 'o' = 111 'u' = 117 Example Let's create a function that converts ASCII codes to vowels when they match: const arr = [5, 23, ...
Read MoreFinding the inclination of arrays in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns true if it's either strictly increasing or strictly decreasing, otherwise returns false. In Mathematics, a strictly increasing function is that function in which the value to be plotted always increases. Similarly, a strictly decreasing function is that function in which the value to be plotted always decreases. Understanding the Concept An array has a consistent inclination when: Strictly increasing: Each element is greater than the previous one Strictly decreasing: Each element ...
Read MoreChecking if all array values are smaller than a limit using JavaScript
Problem We are required to write a JavaScript function that takes in an array of numbers and a number. We should return true if all the numbers in the array are smaller than the number given as second argument, false otherwise. Using Array.every() Method The Array.every() method tests whether all elements in the array pass the test implemented by the provided function. It returns true if all elements satisfy the condition, false otherwise. Example 1: All Values Smaller Following is the code − const arr = [5, 34, 23, 14, 78, 45, 78]; const limit = ...
Read MoreCheck if the elements of the array can be rearranged to form a sequence of numbers or not in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and checks if the elements of the array can be rearranged to form a sequence of numbers or not. For example: If the array is − const arr = [3, 1, 4, 2, 5]; Then the output should be true because these numbers can be rearranged to form the consecutive sequence: 1, 2, 3, 4, 5. Approach To solve this problem, we need to: Sort the array in ascending order Check if each element is exactly ...
Read MoreSumming array of string numbers using JavaScript
Problem We are required to write a JavaScript function that takes in an array that contains integers and string numbers. Our function should sum all the integers and string numbers together to derive a new number and return that number. Example Following is the code − const arr = [67, 45, '34', '23', 4, 6, '6']; const mixedSum = (arr = []) => { let sum = 0; for(let i = 0; i < arr.length; i++){ const el = arr[i]; ...
Read MoreFinding the continuity of two arrays in JavaScript
We are required to write a JavaScript function that takes in two arrays of numbers. The function should return true if the two arrays upon combining can form a consecutive sequence, false otherwise. For example: If the arrays are − const arr1 = [4, 6, 2, 9, 3]; const arr2 = [1, 5, 8, 7]; When combined and sorted, these arrays form [1, 2, 3, 4, 5, 6, 7, 8, 9], which is a consecutive sequence. Therefore, the output should be true. Understanding Consecutive Sequences A consecutive sequence means each number is exactly ...
Read MoreReplacing digits to form binary using JavaScript
We need to write a JavaScript function that converts a string of digits into a binary-like representation by replacing digits below 5 with '0' and digits 5 and above with '1'. Problem Statement Given a string containing digits, transform it using these rules: Digits 0, 1, 2, 3, 4 → '0' Digits 5, 6, 7, 8, 9 → '1' Using For Loop const str = '262355677834342'; const convert = (str = '') => { let res = ''; for(let i = 0; i < ...
Read MorePerforming the subtraction operation without the subtraction operator in JavaScript
We are required to write a JavaScript function that takes in two numbers and returns their difference but without using the (-) sign. This problem can be solved using bitwise operations. The key insight is that subtraction can be performed using XOR (^) and AND (&) operations combined with bit shifting. How It Works The algorithm uses these bitwise operations: XOR (^): Performs subtraction without handling borrows AND (&): Identifies positions where borrowing is needed Left shift ( { console.log(`Step ${step}: a=${a}, b=${b}`); if(b === 0){ console.log(`Result: ${a}`); return a; } const xor = a ^ b; const borrow = (~a & b)
Read MoreReturning the nth even number using JavaScript
We are required to write a JavaScript function that takes in a number n and returns the nth even number in the sequence of natural numbers. Problem Given a positive integer n, find the nth even number. For example, the 1st even number is 2, the 2nd even number is 4, the 3rd even number is 6, and so on. Understanding the Pattern Even numbers follow a simple pattern: 2, 4, 6, 8, 10, 12... The nth even number can be calculated using the formula: n × 2. Example Implementation Here's how to implement ...
Read MoreDynamic programming to check dynamic behavior of an array in JavaScript
We are required to write a JavaScript function that takes in an array of strings, ordered by ascending length. The function should return true if, for each pair of consecutive strings, the second string can be formed from the first by adding a single letter either at the beginning or end. For example: If the array is given by − const arr = ["c", "ca", "can", "acan", "acane", "dacane"]; Then our function should return true because: "ca" = "c" + "a" (adding "a" at the end) ...
Read More