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 384 of 840
DNA to RNA conversion using JavaScript
Deoxyribonucleic acid (DNA) is the primary information storage molecule in biological systems. It is composed of four nucleic acid bases: Guanine ('G'), Cytosine ('C'), Adenine ('A'), and Thymine ('T'). Ribonucleic acid (RNA) is the primary messenger molecule in cells. RNA differs slightly from DNA in its chemical structure and contains no Thymine. In RNA, Thymine is replaced by another nucleic acid called Uracil ('U'). Problem We need to write a JavaScript function that translates a given DNA string into RNA by replacing all 'T' nucleotides with 'U' nucleotides. Using For Loop The most straightforward approach ...
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 MoreFetch alternative even values from a JavaScript array?
To fetch alternative even values from a JavaScript array, you need to iterate through the array and check if the index is even using the modulo operator. An index is even when index % 2 == 0. Syntax if (index % 2 == 0) { // This will select elements at even positions (0, 2, 4, ...) } Example The following example demonstrates how to fetch alternative even values from an array: var subjectsName = ["MySQL", "JavaScript", "MongoDB", "C", "C++", "Java"]; for (let index = 0; index ...
Read MoreMerge JavaScript objects with the same key value and count them
Suppose we have an array of objects like this: const arr = [{ "value": 10, "id": "111", "name": "BlackCat", }, { "value": 10, "id": "111", "name": "BlackCat", }, { "value": 15, "id": "777", "name": "WhiteCat", }]; We are required to write a JavaScript function that takes in one such array. The function should then merge all those objects together that ...
Read MoreChecking the validity of parentheses in JavaScript
We are required to write a JavaScript function that takes in a string containing just the characters: '(', ')', '{', '}', '[' and ']' Our function should determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. ...
Read MoreSum of All Possible Odd Length Subarrays in JavaScript
We are required to write a JavaScript function that takes in an array of integers as the only argument. The function should first permute all possible subarrays from the original array that have an odd length. And then the function should find the combined sum of all the elements of those subarrays and return the sum. For example, if the input array is: const arr = [1, 2, 3]; Then the output should be: const output = 12; Because the desired subarrays are [1], [2], [3], [1, 2, 3] with ...
Read MoreProblem Can we fit remaining passengers in the bus using JavaScript
Problem We need to write a JavaScript function that determines if a bus can accommodate all waiting passengers. The function takes three parameters: cap − the total capacity of people the bus can hold (excluding the driver) on − the number of people currently on the bus (excluding the driver) wait − the number of people waiting to board the bus If there's enough space for everyone, return 0. Otherwise, return the number of passengers who cannot board. Solution The logic is straightforward: calculate ...
Read MoreLongest subarray with unit difference in JavaScript
We need to find the length of the longest subarray where the difference between maximum and minimum values is exactly 1. This means the subarray can only contain two consecutive numbers (like 3 and 4) or just one unique number repeated. Problem Statement Given an array of numbers, find the length of the longest subarray where the difference between its maximum and minimum values is exactly 1. For example, with input array: const arr = [2, 4, 3, 3, 6, 3, 4, 8]; The longest valid subarray is [4, 3, 3, 3, 4] ...
Read MoreHow to allocate memory to an object whose length is set to 0 - JavaScript?
In JavaScript, you can manipulate array length to clear memory and allocate new slots. When you set an array's length to 0, it clears all elements. Setting length to a larger value allocates empty slots. Understanding Array Length Property The length property controls how many elements an array can hold. Setting it to 0 removes all elements, while setting it to a larger number creates empty slots filled with undefined. var arrayObject = [ "John", "David", "Mike" ]; console.log("Original array:", arrayObject); console.log("Original ...
Read MoreFinding product of an array using recursion in JavaScript
We are required to write a JavaScript function that takes in an array of numbers. Our function should do the following two things: Make use of a recursive approach. Calculate the product of all the elements in the array. And finally, it should return the product. For example, if the input array is: const arr = [1, 3, 6, 0.2, 2, 5]; Then the output should be: 36 How Recursion Works Recursion breaks down the problem into smaller subproblems. For ...
Read More