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 160 of 534
Checking 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 MoreNumber of carries required while adding two numbers in JavaScript
When adding two numbers on paper, we sometimes need to "carry" a digit to the next column when the sum of digits exceeds 9. This article shows how to count the total number of carries required during addition. Problem We need to write a JavaScript function that takes two numbers and counts how many carries are needed when adding them digit by digit, just like manual addition on paper. For example, when adding 179 and 284: 9 + 4 = 13 (carry 1) 7 + 8 + 1 = 16 (carry 1) 1 + 2 + ...
Read MoreFinding the immediate bigger number formed with the same digits in JavaScript
We need to write a JavaScript function that takes a number and rearranges its digits to form the smallest number that is just bigger than the input number using the same digits. For instance, if the input number is 112, the output should be 121. If no bigger number can be formed (like 321), we return -1. Algorithm Approach The brute force approach involves: Find the maximum possible number by sorting digits in descending order Check each number from input+1 to maximum Return the first number that uses the same digits Example Implementation ...
Read MoreNumber difference after interchanging their first digits in JavaScript
We are required to write a JavaScript function that takes in an array of exactly two numbers. Our function should return the absolute difference between the numbers after interchanging their first digits. For instance, for the array [105, 413]: Original numbers: 105 and 413 After interchanging first digits: 405 and 113 The difference will be: |405 - 113| = 292 Algorithm The approach involves converting numbers to strings, extracting first digits, creating new numbers with swapped first digits, and calculating the absolute difference. Example const arr = [105, 413]; const ...
Read More