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 87 of 534
Finding the date at which money deposited equals a specific amount in JavaScript
When calculating compound interest over time, you may need to determine the exact date when your deposited money will reach a target amount. This problem involves daily compounding at a given annual interest rate. Problem Statement Given an initial deposit amount, a target amount, and an annual interest rate, we need to find the date when the deposited money (with daily compounding) will equal or exceed the target amount. The calculation starts from January 1st, 2021, with interest compounded daily at a rate of p percent divided by 360 days. Solution Approach The solution uses a ...
Read MoreChecking 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 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 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 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 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 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 MoreFinding distance between two points in a 2-D plane using JavaScript
Problem We are required to write a JavaScript function that takes in two objects both having x and y property specifying two points in a plane. Our function should find and return the distance between those two points using the Euclidean distance formula. Distance Formula The distance between two points (x₁, y₁) and (x₂, y₂) is calculated using: distance = √[(x₂ - x₁)² + (y₂ - y₁)²] Example Following is the code: const a = {x: 5, y: -4}; const b = {x: 8, y: 12}; const distanceBetweenPoints ...
Read MoreBinary array to corresponding decimal in JavaScript
Problem We are required to write a JavaScript function that takes in a binary array (consisting of only 0 and 1). Our function should first join all the bits in the array and then return the decimal number corresponding to that binary. Method 1: Using Math.pow() with Manual Calculation This approach calculates the decimal value by iterating through the array and using powers of 2: const arr = [1, 0, 1, 1]; const binaryArrayToNumber = arr => { let num = 0; for (let i = ...
Read MoreConverting km per hour to cm per second using JavaScript
We are required to write a JavaScript function that takes in a number that specifies speed in kmph and it should return the equivalent speed in cm/s. Understanding the Conversion To convert km/h to cm/s, we need to understand the relationship: 1 kilometer = 100, 000 centimeters 1 hour = 3, 600 seconds Formula: km/h × (100, 000 cm / 1 km) × (1 hour / 3, 600 seconds) Example Following is the code: const kmph = 12; const convertSpeed = (kmph) => { const secsInHour = ...
Read More