Found 10483 Articles for Web Development

Check whether sum of digit of a number is Palindrome - JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:41:24

628 Views

We are required to write a JavaScript function that takes in a number, sums its digits and checks whether that sum is a Palindrome number or not. The function should return true if the sum is Palindrome, false otherwise.For example, if the number is 697, then the sum of its digit will be 22, which indeed, is a Palindrome number.Therefore, our function should return true for 697.ExampleFollowing is the code −const num = 697; const sumDigit = (num, sum = 0) => {    if(num){       return sumDigit(Math.floor(num / 10), sum + (num % 10));    };   ... Read More

Swap kth element of array - JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:39:36

609 Views

We are required to write a JavaScript function that accepts an array of Numbers and a number, say k (k must be less than or equal to the length of array).And our function should replace the kth element from the beginning with the kth element from the end of the array.ExampleFollowing is the code −const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const swapKth = (arr, k) => {    const { length: l } = arr;    let temp;    const ind = k-1;    temp = arr[ind];    arr[ind] = arr[l-k];    arr[l-k] = temp; }; swapKth(arr, 4); console.log(arr); swapKth(arr, 8); console.log(arr);OutputFollowing is the output in the console −[    0, 1, 2, 6, 4,    5, 3, 7, 8, 9 ] [    0, 1, 7, 6, 4,    5, 3, 2, 8, 9 ]

Split string into groups - JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:37:43

270 Views

Given a string S which consists of alphabets, numbers and special characters.We need to write a program to split the strings in three different strings S1, S2 and S3, such thatThe string S1 will contain all the alphabets present in S, The string S2 will contain all the numbers present in S, andS3 will contain all special characters present in SThe strings S1, S2 and S3 should have characters in the same order as they appear in input.ExampleFollowing is the code −const str = "Th!s String C0nt@1ns d1fferent ch@ract5rs"; const seperateCharacters = str => {    const strArr = str.split(""); ... Read More

JavaScript code to find nth term of a series - Arithmetic Progression (AP)

Disha Verma
Updated on 25-Feb-2025 14:50:36

1K+ Views

To find the nth term of an arithmetic progression in JavaScript, we need to write a function that will calculate the nth term. In this article, we will explore how to find the nth term of an AP using JavaScript. We are required to write a JavaScript function that takes in three numbers as arguments (the first two numbers are supposed to be the starting two consecutive terms of an arithmetic progression). And the third number, say n, is the 1 index-based element of the series whose value we must calculate. If the input is 2, 5, 7 Then ... Read More

Count number of factors of a number - JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:33:47

459 Views

We are required to write a JavaScript function that takes in a number and returns the count of numbers that exactly divides the input number.For example −If the number is 12, then its factors are −1, 2, 3, 4, 6, 12Therefore, the output should be 6.ExampleFollowing is the code −const num = 12; const countFactors = num => {    let count = 0;    let flag = 2;    while(flag

Load a text file and find number of characters in the file - JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:31:53

1K+ Views

Suppose we have a data.txt file that lives in the same directory as our NodeJS file. Suppose the content of that file is −Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s.We are required to write a JavaScript function that loads this external ... Read More

Largest and smallest word in a string - JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:28:43

1K+ Views

We are required to write a JavaScript function that takes in string and returns an array with two string values, and they should be the smallest and largest words respectively from the string.For example −If the string is −const str = "Hardships often prepare ordinary people for an extraordinary destiny";Then the output should be −const output = ["an", "extraordinary"];So, let's write the code for this functionExampleFollowing is the code −const str = "Hardships often prepare ordinary people for an extraordinary destiny"; const largestSmallest = str => {    const strArr = str.split(" ");    let min = strArr[0];    let ... Read More

Finding area of triangle in JavaScript using Heron’s formula

AmitDiwan
Updated on 15-Sep-2020 11:25:16

887 Views

We are given the lengths of three sides of a triangle and we are required to write a function that returns the area of the triangle using the length of its sides.Heron's formulaWe can calculate the area of a triangle if we know the lengths of all three sides, using Heron's formula −Step 1 − Calculate "s" (half of the triangles perimeter) −s = (a+b+c) / 2Step 2 − Then calculate the Area using Herons formula −A = sqrt( s(s-a)(s-b)(s-c) )ExampleSo, Let’s write the code for this function −const sides = [12, 4, 9]; const areaOfTriangle = sides => { ... Read More

Nearest power 2 of a number - JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:23:47

862 Views

We are required to write a JavaScript function that takes in a number and returns a number that can be represented as a power of 2 which is nearest to the input number.For example −If the input number if 365, then the output should be 256, because 256 is the nearest such number to 365 which can be represented as 2^n for some whole number value of n.ExampleLet’s write the code for this function −const num = 365; const nearestPowerOfTwo = num => {    // dealing only with non-negative numbers    if(num < 0){       num *= ... Read More

Finding roots of a quadratic equation – JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:21:57

519 Views

We are required to write a JavaScript function that takes in three numbers (representing the coefficient of quadratic term, coefficient of linear term and the constant respectively in a quadratic quadratic).And we are required to find the roots, (if they are real roots) otherwise we have to return false. Let's write the code for this functionExampleFollowing is the code −const coefficients = [3, 12, 2]; const findRoots = co => {    const [a, b, c] = co;    const discriminant = (b * b) - 4 * a * c;    if(discriminant < 0){       // the ... Read More

Advertisements