Found 10483 Articles for Web Development

Convert an array of objects into plain object in JavaScript

AmitDiwan
Updated on 18-Jan-2021 04:45:35

614 Views

Suppose we have an array of objects like this −const arr = [{    name: 'Dinesh Lamba',    age: 23,    occupation: 'Web Developer', }, {    address: 'Vasant Vihar',    experience: 5,    isEmployed: true }];We are required to write a JavaScript function that takes in one such array of objects. The function should then prepare an object that contains all the properties that exist in all the objects of the array.Therefore, for the above array, the output should look like −const output = {    name: 'Dinesh Lamba',    age: 23,    occupation: 'Web Developer',    address: 'Vasant ... Read More

Finding n subarrays with equal sum in JavaScript

AmitDiwan
Updated on 18-Jan-2021 04:43:45

136 Views

We are required to write a JavaScript function that takes in an array of integers as the first argument and an integer as the second argument.The function should check whether we can create n (second argument) subarrays from the original array such that all the subarrays have the equal sum.For example −If the inputs are −const arr = [4, 3, 2, 3, 5, 2, 1]; const num = 4;The output should be true because the subarrays are: [5], [1, 4], [2, 3], [2, 3] all having sum equal to 5.ExampleFollowing is the code −const arr = [4, 3, 2, 3, ... Read More

Tribonacci series in JavaScript

AmitDiwan
Updated on 18-Jan-2021 04:40:12

1K+ Views

Tribonacci Series:The tribonacci sequence is a generalization of the Fibonacci sequence where each term is the sum of the three preceding terms.For example, the first few terms of the tribonacci series are −0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149We are required to write a JavaScript function that takes in a number, say num, as the only argument.The function should then return an array of num elements, containing the first num terms of the tribonacci series.For example:f(6) = 0, ExampleFollowing is the code:const tribonacci = (num = 1) => {    if (num === 0 || num ... Read More

Construct an identity matrix of order n in JavaScript

AmitDiwan
Updated on 11-Dec-2020 09:47:53

880 Views

Identity MatrixAn identity Matrix is a matrix which is n × n square matrix where the diagonal consist of ones and the other elements are all zeros.For example an identity matrix of order is will be −const arr = [    [1, 0, 0],    [0, 1, 0],    [0, 0, 1] ];We are required to write a JavaScript function that takes in a number, say n, and returns an identity matrix of n*n order.ExampleFollowing is the code −const num = 5; const constructIdentity = (num = 1) => {    const res = [];    for(let i = 0; ... Read More

Return Vowels in a string in JavaScript

AmitDiwan
Updated on 11-Dec-2020 09:46:17

773 Views

We are required to write a JavaScript function that takes in a string that might contain some alphabets. The function should count and return the number of vowels that exists in the string.ExampleFollowing is the code −const str = 'this is a string'; const countVowels = (str = '') => {    str = str.toLowerCase();    const legend = 'aeiou';    let count = 0;    for(let i = 0; i < str.length; i++){       const el = str[i];       if(!legend.includes(el)){          continue;       };       count++;    };    return count; }; console.log(countVowels(str));OutputFollowing is the output on console −4

JavaScript function that generates all possible combinations of a string

AmitDiwan
Updated on 11-Dec-2020 09:45:26

791 Views

We are required to write a JavaScript function that takes in a string as the only argument. The function should generate an array of strings that contains all possible contiguous substrings that exist in the array.ExampleFollowing is the code −const str = 'Delhi'; const allCombinations = (str1 = '') => {    const arr = [];    for (let x = 0, y=1; x < str1.length; x++,y++) {       arr[x]=str1.substring(x, y);    };    const combination = [];    let temp= "";    let len = Math.pow(2, arr.length);    for (let i = 0; i < len ; i++){       temp= "";       for (let j=0;j

JavaScript function to convert Indian currency numbers to words with paise support

AmitDiwan
Updated on 11-Dec-2020 09:44:14

3K+ Views

We are required to write a JavaScript function that takes in a floating-point number up to 2 digits of precision.The function should convert that number into the Indian current text.For example −If the input number is −const num = 12500Then the output should be −const output = 'Twelve Thousand Five Hundred';ExampleFollowing is the code −const num = 12500; const wordify = (num) => {    const single = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"];    const double = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"];    const tens = ["", "Ten", "Twenty", "Thirty", ... Read More

Calculate the hypotenuse of a right triangle in JavaScript

AmitDiwan
Updated on 11-Dec-2020 09:40:27

713 Views

We are required to write a JavaScript function that takes in two numbers. The first number represents the length of the base of a right triangle and the second is perpendicular. The function should then compute the length of the hypotenuse based on these values.For example −If base = 8, perpendicular = 6Then the output should be 10ExampleFollowing is the code −const base = 8; const perpendicular = 6; const findHypotenuse = (base, perpendicular) => {    const bSquare = base ** 2;    const pSquare = perpendicular ** 2;    const sum = bSquare + pSquare;    const hypotenuse = Math.sqrt(sum);    return hypotenuse; }; console.log(findHypotenuse(base, perpendicular)); console.log(findHypotenuse(34, 56));OutputFollowing is the output on console −10 65.5133574166368

Removing all non-alphabetic characters from a string in JavaScript

Disha Verma
Updated on 18-Mar-2025 12:59:47

871 Views

Non-alphabetic characters are any characters that are not part of the alphabet, and the strings are a data type that represents a sequence of characters or text. Working with strings is a basic task in JavaScript, and one common task is removing all non-alphabetic characters from a string. In this article, we will understand how to remove all non-alphabetic characters from a string. Understanding the Problem Suppose we have a string saying "he@656llo wor?ld". We want to remove all digits, punctuation, whitespace, and special characters from the given string. For example: Input string we have: "he@656llo wor?ld" ... Read More

Converting ASCII to hexadecimal in JavaScript

AmitDiwan
Updated on 11-Dec-2020 09:38:13

902 Views

We are required to write a JavaScript function that takes in a string that represents a ASCII number. The function should convert the number into its corresponding hexadecimal code and return the hexadecimal.For example −f the input ASCII string is −const str = '159';Then the hexadecimal code for this should be 313539.ExampleFollowing is the code −const str = '159'; const convertToHexa = (str = '') =>{    const res = [];    const { length: len } = str;    for (let n = 0, l = len; n < l; n ++) {       const hex = ... Read More

Advertisements