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
Articles by AmitDiwan
Page 381 of 840
Reversing the alphabet from back to front and front to back in JavaScript
In JavaScript, we can create a function that finds the "mirror" position of an alphabet character. Given a letter, we find its position from the start and return the letter at the same position from the end. Understanding the Logic The alphabet has 26 letters. If we split it into two halves, each letter in the first half corresponds to a letter in the second half when reversed. For example, 'a' (1st position) corresponds to 'z' (26th position), 'b' corresponds to 'y', and so on. Example Implementation Here's how we can implement this functionality: ...
Read MoreFinding words in a matrix in JavaScript
We need to write a JavaScript function that takes a 2D character matrix and a string, then determines if the string can be formed by connecting adjacent characters in the matrix without reusing any position. The function searches for a path through the matrix where each character in the target string appears in sequence through adjacent cells (up, down, left, right). Each cell can only be used once per word search. Problem Example Given this matrix and target string: const arr = [ ['s', 'd', 'k', 'e'], ...
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 MoreAddition multiplication ladder in an array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns the alternative multiplicative sum of the elements. For example: If the array is − const arr = [1, 2, 3, 4, 5, 6, 7]; Then the output should be calculated like this − 1*2+3*4+5*6+7 2+12+30+7 And the output should be − 51 Let's write the code for this function − How It Works The algorithm pairs elements at even indices with their next element, multiplies them, and adds ...
Read MoreProgram to make vowels in string uppercase and change letters to next letter in alphabet (i.e. z->a) in JavaScript
We need to write a JavaScript function that takes a string and performs two transformations: convert vowels to uppercase and shift each letter to the next letter in the alphabet (with 'z' wrapping to 'a'). The function should construct a new string based on the input string where all vowels are uppercased and each alphabet character moves to the next letter in sequence. Problem Understanding For example, if the input string is: const str = 'newString'; The expected output should be: const output = 'oExSusIoh'; Here's how the transformation ...
Read MoreFinding all possible subsets of an array in JavaScript
Finding all possible subsets of an array is a common algorithmic problem. A subset is any combination of elements from the original array, including the empty set and the full array itself. For an array of n elements, there are 2^n possible subsets. This is because each element can either be included or excluded from a subset. For example, if the input array is: const arr = [1, 2, 3]; The output should contain all possible subsets: [ [], [1], [2], [3], ...
Read MoreFinding average age from array of Objects using JavaScript
Problem We need to write a JavaScript function that takes an array of objects containing people's data and calculates the average age from the age property. Example Following is the code: const people = [ { fName: 'Ashish', age: 23 }, { fName: 'Ajay', age: 21 }, { fName: 'Arvind', age: 26 }, { fName: 'Mahesh', age: 28 }, { fName: 'Jay', age: 19 }, ]; const findAverageAge = (arr = []) => { ...
Read MoreChanging ternary operator into non-ternary - JavaScript?
The ternary operator provides a concise way to write conditional expressions, but sometimes you need to convert it to regular if-else statements for better readability or debugging purposes. Understanding the Ternary Operator The ternary operator follows this syntax: condition ? valueIfTrue : valueIfFalse. It's a shorthand for simple if-else statements. Converting Ternary to If-Else Here's how to convert a ternary operator into a standard if-else statement: // Using ternary operator var number1 = 12; var number2 = 12; var result = (number1 == number2) ? "equal" : "not equal"; console.log("Ternary result:", result); // ...
Read MorePicking all the numbers present in a string in JavaScript
We are required to write a JavaScript function that takes in a string that contains some one-digit numbers in between and the function should return the sum of all the numbers present in the string. Example The code for this will be − const str = 'uyyudfgdfgf5jgdfj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3'; const sumNum = str => { const strArr = str.split(""); let res = 0; for(let i = 0; i < strArr.length; i++){ if(+strArr[i]){ ...
Read MoreHow to store two arrays as a keyvalue pair in one object in JavaScript?
Suppose, we have two arrays of literals of same length like these − const arr1 = ['firstName', 'lastName', 'age', 'address', 'isEmployed']; const arr2 = ['Rahul', 'Sharma', 23, 'Tilak Nagar', false]; We are required to write a JavaScript function that takes in two such arrays. The function should construct an object mapping the elements of the second array to the corresponding elements of the first array. Method 1: Using Array.reduce() We will use the Array.prototype.reduce() method to iterate over the arrays, building the object. const arr1 = ['firstName', 'lastName', 'age', 'address', 'isEmployed']; ...
Read More