Found 10483 Articles for Web Development

Finding least number of notes to sum an amount - JavaScript

AmitDiwan
Updated on 14-Sep-2020 14:39:10

768 Views

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, in this we for 512, our function should return 3, i.e., the total count ... Read More

JavaScript - Remove first n characters from string

Alshifa Hasnain
Updated on 26-Feb-2025 19:37:10

404 Views

In this article, we will learn to calculate the union of two objects in JavaScript. The union of two objects results in a new object that contains all the properties of both objects. What is the Union of Two Objects? The union of two objects is the process of combining the properties of both objects into a single object. For example Input − const obj1 = { name: " ", email: " " }; const obj2 = { name: ['x'], email: ['y']}; Output − const output = { name: {" ", [x]}, email: {" ", [y]} }; Different Approaches ... Read More

Program to append two given strings such that, if the concatenation creates a double character then omit one of the characters - JavaScript

AmitDiwan
Updated on 14-Sep-2020 14:32:14

196 Views

We are required to write a JavaScript function that takes in two strings and concatenates the second string to the first string.If the last character of the first string and the first character of the second string are the same then we have to omit one of those characters. Let’s say the following are our strings in JavaScript −const str1 = 'Food'; const str2 = 'dog';Let’s write the code for this function −const str1 = 'Food'; const str2 = 'dog'; const concatenateStrings = (str1, str2) => {    const { length: l1 } = str1;    const { length: l2 ... Read More

Check for perfect square without using Math libraries - JavaScript

AmitDiwan
Updated on 14-Sep-2020 14:30:36

373 Views

We are required to write a JavaScript function that takes in a number and returns a boolean based on the fact whether or not the number is a perfect square.Examples of perfect square numbers −4, 16, 81, 441, 256, 729, 9801Let’s write the code for this function −const num = 81; const isPerfectSquare = num => {    let ind = 1;       while(ind * ind

Getting tomorrow and day after tomorrow date in JavaScript

AmitDiwan
Updated on 14-Sep-2020 14:28:39

776 Views

Using the Date class of JavaScript whose object new Date() returns a JavaScript date for the current day, we have to find the date of the next two days.This is a fairly simple problem and we can achieve this with a few lines of code. At first, get today’s date −// getting today's date const today = new Date();Let’s write the code for this function −// getting today's date const today = new Date(); // initializing tomorrow with today's date const tomorrow = new Date(today); // increasing a day in tomorrow and setting it to tomorrow tomorrow.setDate(tomorrow.getDate() + 1); const ... Read More

Reduce an array to the sum of every nth element - JavaScript

AmitDiwan
Updated on 14-Sep-2020 14:27:20

272 Views

We are required to write a JavaScript function that takes in an array of numbers and returns the cumulative sum of every number present at the index that is a multiple of n from the array.Let’s write the code for this function −const arr = [1, 4, 5, 3, 5, 6, 12, 5, 65, 3, 2, 65, 9]; const num = 2; const nthSum = (arr, num) => {    let sum = 0;    for(let i = 0; i < arr.length; i++){       if(i % num !== 0){          continue;       };       sum += arr[i];    };    return sum; }; console.log(nthSum(arr, num));OutputFollowing is the output in the console −99Above, we added every 2nd element beginning with index 0 i.e.1+5+5+12+65+2+9 = 99

Find the average of all elements of array except the largest and smallest - JavaScript

AmitDiwan
Updated on 14-Sep-2020 14:25:35

332 Views

We are required to write a JavaScript function that takes in an array of Number and returns the averages of its elements excluding the smallest and largest Number.Let’s write the code for this function −Following is the code −const arr = [1, 4, 5, 3, 5, 6, 12, 5, 65, 3, 2, 65, 9]; const findExcludedAverage = arr => {    const creds = arr.reduce((acc, val) => {       let { min, max, sum } = acc;       sum += val;       if(val > max){          max = val;     ... Read More

Program to test the equality of two arrays - JavaScript

AmitDiwan
Updated on 14-Sep-2020 14:21:04

139 Views

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 −ExampleFollowing 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){       return false;    };    for(let i = 0; i < first.length; i++){     ... Read More

Divide a string into n equal parts - JavaScript

AmitDiwan
Updated on 14-Sep-2020 14:19:06

629 Views

We are required to write a JavaScript function that takes in a string and a number n (such that n exactly divides the length of string). We need to return an array of string of length n containing n equal parts of the string.Let’s write the code for this function −Exampleconst str = 'this is a sample string'; const num = 5; const divideEqual = (str, num) => {    const len = str.length / num;    const creds = str.split("").reduce((acc, val) => {       let { res, currInd } = acc;       if(!res[currInd] || res[currInd].length ... Read More

Find Second most frequent character in array - JavaScript

AmitDiwan
Updated on 14-Sep-2020 14:16:53

588 Views

We are required to write a JavaScript function that takes in a string and returns the character from the string that appears for the second most number of times.Let’s say the following is our array −const arr = [1, 34, 4, 3, 2, 1, 4, 6, 4, 6, 5, 3, 6, 6];So, the frequent character appearing is −6But we want the output to be the second most frequent character i.e.4Let’s write the code for this function −Exampleconst arr = [1, 34, 4, 3, 2, 1, 4, 6, 4, 6, 5, 3, 6, 6]; const secondMostFrequent = arr => {   ... Read More

Advertisements