Javascript Articles

Page 381 of 534

Square every digit of a number - JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 474 Views

We are required to write a JavaScript function that takes in a number and returns a new number in which all the digits of the original number are squared and concatenatedFor example: If the number is −9119Then the output should be −811181because 9^2 is 81 and 1^2 is 1.ExampleFollowing is the code −const num = 9119; const squared = num => {    const numStr = String(num);    let res = '';    for(let i = 0; i < numStr.length; i++){       const square = Math.pow(+numStr[i], 2);       res += square;    };    return res; }; console.log(squared(num));OutputThis will produce the following output in console −811181

Read More

Shift certain array elements to front of array - JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 260 Views

We are required to write a JavaScript function takes in an array of numbers. The function should bring all the 3-digit integers to the front of the array.Let’s say the following is our array of numbers −const numList = [1, 324,34, 3434, 304, 2929, 23, 444];ExampleFollowing is the code −const numList = [1, 324,34, 3434, 304, 2929, 23, 444]; const isThreeDigit = num => num > 99 && num < 1000; const bringToFront = arr => {    for(let i = 0; i < arr.length; i++){       if(!isThreeDigit(arr[i])){          continue;       };       arr.unshift(arr.splice(i, 1)[0]);    }; }; bringToFront(numList); console.log(numList);OutputThis will produce the following output in console −[   444, 304,  324,     1,  34, 3434,  2929,  23 ]

Read More

Sorting an associative array in ascending order - JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 2K+ Views

Suppose, we have an array of objects like this −const people = [    {"id":1, "name":"Andrew", "age":30, "gender":"m", "category":"G"},    {"id":2, "name":"Brandon", "age":25, "gender":"m", "category":"G"},    {"id":3, "name":"Christine", "age":20, "gender":"m", "category":"G"},    {"id":4, "name":"Elena", "age":29, "gender":"W", "category":"M"}   ];We are required to write a JavaScript function that takes in one such array and sorts the array in place, according to the age property of each object in increasing order.Therefore, the output should look something like this −const output = [    {"id":3, "name":"Christine", "age":20, "gender":"m", "category":"G"},    {"id":2, "name":"Brandon", "age":25, "gender":"m", "category":"G"},    {"id":4, "name":"Elena", "age":29, "gender":"W", "category":"M"},    {"id":1, ...

Read More

How to reverse a portion of an array in JavaScript?

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 365 Views

We are required to write a JavaScript function that takes in an array, a start index and an end index. The function should reverse the portion of the array between the start index and end index.For example −If the array is −const arr = [2, 6, 5, 8, 3, 5, 2, 6, 7];And the start index and end index are 3, 7 respectively, then the array should be reversed to −const output = [2, 6, 5, 2, 5, 3, 8, 6, 7];ExampleFollowing is the code −const arr = [2, 6, 5, 8, 3, 5, 2, 6, 7]; const start = ...

Read More

Find the Symmetric difference between two arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 292 Views

In Mathematics, the symmetric difference of two sets, say A and B is represented by A △ BAnd it is defined as the set of all those elements which belongs either to A or to B but not to both.For example −const A = [1, 2, 3, 4, 5, 6, 7, 8]; const B = [1, 3, 5, 6, 7, 8, 9];Then the symmetric difference of A and B will be −const diff = [2, 4, 9]ExampleFollowing is the code −const A = [1, 2, 3, 4, 5, 6, 7, 8]; const B = [1, 3, 5, 6, 7, 8, ...

Read More

Checking whether the sum of digits of a number forms a Palindrome Number or not in JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 164 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 certain element from end and start of array - JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 242 Views

We are required to write a JavaScript function that accepts an array of Numbers and a number, say n (n must be less than or equal to the length of array). And our function should replace the kth element from the beginning with the nth element from the end of the array.ExampleFollowing is the code −const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const swapNth = (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); swapNth(arr, 8); console.log(arr);OutputThis will produce the following output in console −[    0, 1, 2, 6, 4,    5, 3, 7, 8, 9 ] [    0, 1, 7, 6, 4,    5, 3, 2, 8, 9 ]

Read More

Splitting string into groups &ndash; JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 428 Views

Given a string S, consisting 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 that −The 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 S.The 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 = ...

Read More

Split keys and values into separate objects - JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 4K+ Views

Suppose, we have an object like this −const dataset = {    "diamonds":77,    "gold-bars":28,    "exciting-stuff":52,    "oil":51,    "sports-cars":7,    "bitcoins":40 };We are required to write a JavaScript function that takes one such object and returns an array of objects that have keys and their values splitted.Therefore, for the above object, the output should be −const output = [    {"asset":"diamonds", "quantity":77},    {"asset":"gold-bars", "quantity":28},    {"asset":"exciting-stuff", "quantity":52},    {"asset":"oil", "quantity":51},    {"asset":"bitcoins", "quantity":40} ];ExampleFollowing is the code −const dataset = {    "diamonds":77,    "gold-bars":28,    "exciting-stuff":52,    "oil":51,    "sports-cars":7,    "bitcoins":40 }; const splitKeyValue = ...

Read More

Deleting the duplicate strings based on the ending characters - JavaScript

AmitDiwan
AmitDiwan
Updated on 30-Sep-2020 212 Views

We are required to write a JavaScript function that takes in an array of strings and deletes each one of the two strings that ends with the same character −For example, If the actual array is −const arr = ['Radar', 'Cat' , 'Dog', 'Car', 'Hat'];Then we have to delete one and keep only one string ending with the same character in the array of distinct letters.ExampleFollowing is the code −const arr = ['Radar', 'Cat' , 'Dog', 'Car', 'Hat']; const delelteSameLetterWord = arr => {    const map = new Map();    for(let i = 0; i < arr.length; ){   ...

Read More
Showing 3801–3810 of 5,338 articles
« Prev 1 379 380 381 382 383 534 Next »
Advertisements