Found 6683 Articles for Javascript

Mapping string to Numerals in JavaScript

AmitDiwan
Updated on 22-Oct-2020 13:05:36

640 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 = 25Note: Remove any special characters and spaces.So, if the input is −"hello man"Then the output should be −"8, 5, 12, 12, 15, 13, 1, 14"ExampleThe code for this will be −const str = 'hello man'; const charPosition = str => {    str = str.split('');    const arr = [];    const ... Read More

Decimal count of a Number in JavaScript

Nikhilesh Aleti
Updated on 19-Dec-2022 17:09:00

11K+ Views

Given there is a number with digits after decimal point and the task is to find the count of digits after the decimal point. Input Output Scenario Let’s look into the input output scenario, where there is a floating point number having some digits after decimal point. Input = 45.36346323 Output = 8 As we can in the above snippet there are 8 digits in the floating point number after the decimal point. To achieve the above task, we used three methods. Namely isInteger(), toString() and, split() method. Let’s see them one by one – Number.isInteger()method The ... Read More

Finding the smallest fitting number in JavaScript

AmitDiwan
Updated on 22-Oct-2020 13:01:47

101 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.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [4, 6, 34, 76, 78, 44, 34, 26, 88, 76, 42]; const dividesAll = el => {    const result = [];    let num;    for (num = Math.floor(el / 2); num > 1; num--){       if (el % num === 0) {          result.push(num);       }    };    return result; }; const dividesArray = arr => {    return arr.map(dividesAll).reduce((acc, val) => {       return acc.filter(el => val.includes(el));    }); }; console.log(dividesArray(arr));OutputThe output in the console will be −[ 2 ]

Sorting Array without using sort() in JavaScript

AmitDiwan
Updated on 22-Oct-2020 12:59:41

7K+ Views

We are required to write a JavaScript function that takes in an array of numbers.The function should sort the array using the Array.prototype.sort() method, but, here, we are required to use the Array.prototype.reduce() method to sort the array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [4, 56, 5, 3, 34, 37, 89, 57, 98]; const sortWithReduce = arr => {    return arr.reduce((acc, val) => {       let ind = 0;       while(ind < arr.length && val < arr[ind]){          ind++;       }       acc.splice(ind, 0, val);       return acc;    }, []); }; console.log(sortWithReduce(arr));OutputThe output in the console will be −[    98, 57, 89, 37, 34,    5, 56, 4, 3 ]

Squared concatenation of a Number in JavaScript

AmitDiwan
Updated on 22-Oct-2020 12:57:07

178 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 concatenated.For example: If the number is −99Then the output should be −8181because 9^2 is 81 and 1^2 is 1.Therefore, let’s write the code for this function −ExampleThe code for this will be −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);   ... Read More

Reorder an array in JavaScript

Nikhilesh Aleti
Updated on 22-Sep-2022 12:04:11

8K+ Views

The given task is to reorder an array in JavaScript. We can reorder the elements in the array by using the following methods. One of the ways to achieve the above task is b using sort() method. The sort() is an in-built method in JavaScript, which sorts the alphabetic elements. By default, it sorts in ascending order. Example Following is the example where the array got reordered in ascending order − const States = ["Telangana", "Uttar Pradesh", "Karnataka", "Kerala", ... Read More

Repeated sum of Number’s digits in JavaScript

AmitDiwan
Updated on 22-Oct-2020 12:54:30

284 Views

We are required to write a JavaScript function that recursively sums up the digits of a number until it reduces to a single digit number.We are required to do so without converting the number to String or any other data type.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 546767643; const sumDigit = (num, sum = 0) => {    if(num){       return sumDigit(Math.floor(num / 10), sum + (num % 10));    }    return sum; }; const sumRepeatedly = num => {    while(num > 9){       num = sumDigit(num);    };    return num; }; console.log(sumRepeatedly(num));OutputThe output in the console will be −3

Taking common elements from many arrays in JavaScript

AmitDiwan
Updated on 22-Oct-2020 12:50:49

453 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.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = [2, 6, 7, 1, 7, 8, 4, 3]; const arr2 = [5, ,7, 2, 2, 1, 3]; const arr3 = [1, 56, 345, 6, 54, 2, 68, 85, 3]; const intersection = (arr1, arr2) => {    const res = [];    for(let ... Read More

Summing up digits and finding nearest prime in JavaScript

AmitDiwan
Updated on 22-Oct-2020 12:49:13

61 Views

We are required to write a JavaScript function that takes in a number, finds the sum of its digits and returns a prime number that is just greater than or equal to the sum.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 56563; const digitSum = (num, sum = 0) => {    if(num){       return digitSum(Math.floor(num / 10), sum + (num % 10));    }    return sum; }; const isPrime = n => {    if (n===1){       return false;    }else if(n === 2){   ... Read More

From JSON object to an array in JavaScript

Nikhilesh Aleti
Updated on 12-Sep-2023 03:27:16

37K+ Views

The JSON (JavaScript object notation) Object can be created with JavaScript. JSON Object is always surrounded inside the curly brackets {}. The keys must be in strings and values must be in valid JSON data type. The data types like string, number, object, Boolean, array, and Null will be supported by JSON. The keys and values are separated by a colon(":") and a comma separates each key and value pair. Syntax Following is the syntax of JSON Object − var JSONObj = {}; The below example is the basic declaration of a JSON object in JavaScript − var JSONObj ... Read More

Advertisements