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 476 of 534
Take two numbers m and n & return two numbers whose sum is n and product m in JavaScript
We need to write a JavaScript function that takes two numbers m (product) and n (sum) and returns two numbers whose sum equals n and product equals m. If no such numbers exist, the function should return false. This is essentially solving a quadratic equation where we need to find two numbers x and y such that x + y = n and x × y = m. Mathematical Approach We can solve this using the quadratic formula. If x + y = n and x × y = m, then x and y are roots of ...
Read MoreFinding if three points are collinear - JavaScript
Three or more points that lie on the same straight line are called collinear points. In JavaScript, we can determine if three points are collinear by checking if they have the same slope between each pair of points. Mathematical Concept Three points A, B, and C are collinear if the slope between any two pairs is equal: slope of AB = slope of BC = slope of AC Slope Formula For two points A(x1, y1) and B(x2, y2), the slope is calculated as: Slope = (y2 - y1) / (x2 - ...
Read MorePronic numbers in JavaScript
A Pronic number is a number which is the product of two consecutive integers, that is, a number of the form n(n + 1). For example, 6 is a Pronic number because 6 = 2 × 3, and 12 is Pronic because 12 = 3 × 4. We are required to write a JavaScript function that takes in a number and returns true if it is a Pronic number otherwise returns false. Understanding Pronic Numbers The first few Pronic numbers are: 0, 2, 6, 12, 20, 30, 42, 56, 72, 90... ...
Read MoreProgram to find uncommon elements in two arrays - JavaScript
Finding uncommon elements in two arrays means identifying elements that exist in one array but not in both. This is also known as finding the symmetric difference between two arrays. Problem Definition Given two arrays, we need to find elements that are present in either the first array or the second array, but not in both arrays. const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34]; console.log("Array 1:", arr1); console.log("Array 2:", arr2); Array 1: [12, 54, 2, 4, 6, 34, ...
Read MoreCount appearances of a string in another - JavaScript
We are required to write a JavaScript function that takes in two strings and returns the count of the number of times the first string appears in the second string. Let's say our string is: const main = 'This is the is main is string'; We have to find the appearance of the below string in the above "main" string: const sub = 'is'; Using Regular Expression with replace() Let's write the code for this function using a regular expression approach: const main = 'This is the is ...
Read MoreProgram to test the equality of two arrays - JavaScript
We are required to write a JavaScript function that takes in two arrays of literals and checks the corresponding elements of the array and it should return true if all the corresponding elements of the array are equal otherwise it should return false. Let's write the code for this function − Example Following is the code − const arr1 = [1, 4, 5, 3, 5, 6]; const arr2 = [1, 4, 5, 2, 5, 6]; const areEqual = (first, second) => { if(first.length !== second.length){ ...
Read MoreGetting tomorrow and day after tomorrow date in JavaScript
Using the Date class in JavaScript, we can easily calculate tomorrow and the day after tomorrow from the current date. The new Date() constructor returns a JavaScript Date object for the current day. Getting Today's Date First, let's get today's date using the Date constructor: const today = new Date(); console.log("Today:", today.toDateString()); Today: Thu Aug 13 2020 Method 1: Using setDate() with Date Copying We can create new Date objects by copying today's date and then adding days using the setDate() method: // Getting today's date const today ...
Read MoreFinding least number of notes to sum an amount - JavaScript
Suppose, we have a currency system where we have denominations of 1000 units, 500 units, 100 units, 50 units, 20 units, 10 units, 5 units, 2 units and 1 unit. Given a specific amount, we are required to write a function that calculates the least number of total denominations that sum up to the amount. For example, if the amount is 512, The least number of notes that will add up to it will be: 1 unit of 500, 1 unit of 10 and 1 unit of 2. So, for 512, our function should ...
Read MoreComparing ascii scores of strings - JavaScript
ASCII is a 7-bit character encoding standard where every character has a unique decimal code. In JavaScript, we can use the charCodeAt() method to get the ASCII value of any character. This article demonstrates how to compare two strings by calculating their ASCII scores (the sum of ASCII values of all characters) and finding the difference between them. Understanding ASCII Values Each character has a specific ASCII decimal value: console.log('A'.charCodeAt(0)); // 65 console.log('a'.charCodeAt(0)); // 97 console.log(' '.charCodeAt(0)); // 32 (space) console.log('1'.charCodeAt(0)); // 49 65 97 32 49 ...
Read MoreConvert nested array to string - JavaScript
Converting a nested array to a string in JavaScript involves flattening all nested elements and concatenating their values. This is commonly needed when processing complex data structures. Problem Statement We need to write a JavaScript function that takes a nested array of literals and converts it to a single string by concatenating all values, regardless of nesting depth. const arr = [ 'hello', [ 'world', 'how', [ 'are', 'you', [ ...
Read More