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 258 of 801
Finding path to the end of maze using JavaScript
Problem We are required to write a JavaScript function that takes in a matrix of N * N order. The walls in the matrix are marked by 'W' and empty positions are marked by '_'. We can move in any of the four directions at any point. Our function should return true if we can reach the end position [N - 1, N - 1] from the starting position [0, 0], false otherwise. Algorithm Approach We'll use a Breadth-First Search (BFS) algorithm to find if a path exists from the starting position to the end position. ...
Read MoreImplement a custom function similar to Array.prototype.includes() method using JavaScript
We need to create a custom JavaScript function that mimics the behavior of Array.prototype.includes(). This function should check if a value exists in an array and return a boolean result. Problem We are required to write a JavaScript function that lives on the prototype object of Array. It must take in a literal value, and return true if that value is present in the array it is being called upon, false otherwise. Basic Implementation Here's a simple implementation using a for loop to iterate through the array: const arr = [1, 2, 3, 4, ...
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 MoreChecking existence of all continents in array of objects in JavaScript
Problem We are required to write a JavaScript function that takes in an array of objects that contains data about the continent of belonging for some people. Our function should return true if it finds six different continents in the array of objects, false otherwise. Example Following is the code − const people = [ { firstName: 'Dinesh', lastName: 'A.', country: 'Algeria', continent: 'Africa', age: 25, language: 'JavaScript' }, { firstName: 'Ishan', lastName: 'M.', country: 'Chile', continent: 'South America', age: 37, language: 'C' }, ...
Read MoreReturning array of natural numbers between a range in JavaScript
We need to write a JavaScript function that takes an array of two numbers [a, b] (where a ≤ b) and returns an array containing all natural numbers within that range, including the endpoints. Problem Given a range specified by two numbers, we want to generate all natural numbers between them. Natural numbers are positive integers starting from 1. Using a For Loop The most straightforward approach uses a for loop to iterate through the range and build the result array: const range = [6, 10]; const naturalBetweenRange = ([lower, upper] = [1, ...
Read MoreDNA 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 MoreFinding astrological signs based on birthdates using JavaScript
We are required to write a JavaScript function that takes in a date object and returns the astrological sign related to that birthdate based on zodiac date ranges. Understanding Zodiac Signs Each zodiac sign corresponds to specific date ranges throughout the year. The challenge is handling the transition dates correctly, especially for signs that span across months. Example Following is the code: const date = new Date(); // as on 2 April 2021 const findSign = (date) => { const days = [21, 20, 21, 21, 22, 22, 23, 24, 24, 24, 23, 22]; const signs = ["Aquarius", "Pisces", "Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn"]; let month = date.getMonth(); let day = date.getDate(); if(month == 0 && day
Read MoreImplementing custom function like String.prototype.split() function in JavaScript
We need to create a custom function that behaves like the built-in String.prototype.split() method. This function will split a string into an array based on a separator character or string. Problem Statement We are required to write a JavaScript function that extends the String prototype. The function should take a string separator as an argument and return an array of parts where the original string is split by that separator. Implementation Here's how to implement a custom split function: String.prototype.customSplit = function(sep = '') { const res = []; ...
Read MoreFinding sum of sequence upto a specified accuracy using JavaScript
We need to find the sum of a sequence where each term is the reciprocal of factorials. The sequence is: 1/1, 1/2, 1/6, 1/24, ... where the nth term is 1/n!. Understanding the Sequence The sequence can be written as: 1st term: 1/1! = 1/1 = 1 2nd term: 1/2! = 1/2 = 0.5 3rd term: 1/3! = 1/6 ≈ 0.167 4th term: 1/4! = 1/24 ≈ 0.042 Each term is 1 divided by the factorial of its position number. Mathematical Formula Sum = 1/1! + 1/2! + 1/3! + ... + 1/n! Implementation const num = 5; const seriesSum = (n = 1) => { let sum = 0; let factorial = 1; for (let i = 1; i
Read MoreFinding smallest sum after making transformations in JavaScript
We need to write a JavaScript function that takes an array of positive integers and applies transformations until no more are possible. The transformation rule is: if arr[i] > arr[j], then arr[i] = arr[i] - arr[j]. After all transformations, we return the sum of the array. Problem The key insight is that this transformation process eventually reduces all numbers to their Greatest Common Divisor (GCD). When we repeatedly subtract smaller numbers from larger ones, we're essentially performing the Euclidean algorithm. if arr[i] > arr[j] then arr[i] = arr[i] - arr[j] How It Works ...
Read More