AmitDiwan has Published 10740 Articles

How to replace leading zero with spaces - JavaScript

AmitDiwan

AmitDiwan

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

346 Views

We are required to write a JavaScript function that takes in a string that represents a number. Replace the leading zero with spaces in the number. We need to make sure the prior spaces in number are retained.For example, If the string value is defined as −"   004590808"Then the ... Read More

Finding square root of a number without using library functions - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 14:35:30

653 Views

We are required to write a JavaScript function that takes in a number and calculates its square root without using the Math.sqrt() function.ExampleFollowing is the code −const square = (n, i, j) => {    let mid = (i + j) / 2;    let mul = mid * mid; ... Read More

Trying to get number for each character in string - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 14:34:18

237 Views

We are required to write a JavaScript function that takes in a string. It should print out each number for every corresponding letter in the string.For example, a = 1 b = 2 c = 3 d = 4 e = 5 . . . Y = 25 Z = ... Read More

Counting numbers after decimal point in JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 14:33:12

2K+ Views

We are required to write a JavaScript function that takes in a number which may be an integer or a floating-point number. If it's a floating-point number, we have to return the count of numbers after the decimal point. Otherwise we should return 0.For our example, we are considering two ... Read More

How to get the numbers which can divide all values in an array - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 14:32:22

471 Views

We are required to write a JavaScript function that takes in an array of numbers and returns a number which can exactly divide all the numbers in the array.Let’s say the following is our array −const arr = [4, 6, 34, 76, 78, 44, 34, 26, 88, 76, 42];ExampleFollowing is ... Read More

Sorting Array with JavaScript reduce function - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 14:31:21

2K+ Views

We are required to write a JavaScript function that takes in an array of numbers. The function should sort the array with using the Array.prototype.sort() method. We are required to use the Array.prototype.reduce() method to sort the array.Let’s say the following is our array −const arr = [4, 56, 5, ... Read More

Square every digit of a number - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 14:30:20

450 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 ... Read More

Shift certain array elements to front of array - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 14:29:27

248 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 ... Read More

Finding intersection of multiple arrays - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 14:22:16

1K+ Views

We are required to write a JavaScript function that takes in any arbitrary number of arrays and returns an array of elements that are common to all arrays. If there are no common elements, then we should return an empty array.Let’s say the following are our arrays −const arr1 = ... Read More

Sorting an associative array in ascending order - JavaScript

AmitDiwan

AmitDiwan

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

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 ... Read More

Advertisements