Found 10483 Articles for Web Development

Snail Trail Problem in JavaScript

AmitDiwan
Updated on 22-Oct-2020 13:23:23

338 Views

Suppose we have an array like this −const arr = [    [1, 2, 3, 4],    [12, 13, 14, 5],    [11, 16, 15, 6],    [10, 9, 8, 7] ];The array is bound to be a square matrix.We are required to write a JavaScript function that takes in this array and constructs a new array by taking elements and spiraling in until it converges to center. A snail trail spiraling around the outside of the matrix and inwards.Therefore, the output for the above array should be −const output = [1, 2, 3, 4, 5, 6, 7, 8, 9, ... Read More

Best way to find length of JSON object in JavaScript

Nikhilesh Aleti
Updated on 22-Sep-2022 12:19:37

17K+ Views

In this article, the given task is to get the best way of finding the length of a JSON object in JavaScript. Input-Output Scenario Let’s look into this input-output scenario. Consider there is an object with some keys and values in it. we need to get the length of the object. const MyObj = { Name: "Mike", Age: 34 }; document.write(Object.keys(MyObj).length); // Output: 2 Using Object.keys() The Object.keys() method will return the output in form of array of a given object’s own enumerable properties names. It will return the output of keys ... Read More

Removing property from a JSON object in JavaScript

Nikhilesh Aleti
Updated on 23-Sep-2022 11:16:00

7K+ Views

A JSON (JavaScript object notation) Object is always surrounded by the curly braces {}. The values must be a valid JSON data type, and the keys must be strings. JSON supports the following data types: number, string, object, Boolean, array, and null. There has to be a colon (":") separating the keys and values. A comma separates each key and value pair. Following is the syntax of JSON Object − JSONObject = {} Following is the basic declaration of JSON Object in JavaScript − JSONObject = { "name" : "Suriya", ... Read More

Using one array to help filter the other in JavaScript

AmitDiwan
Updated on 22-Oct-2020 13:16:14

101 Views

Suppose, we have an array and objects like these −Objects:const main = [    {name: "Karan", age: 34},    {name: "Aayush", age: 24},    {name: "Ameesh", age: 23},    {name: "Joy", age: 33},    {name: "Siddarth", age: 43},    {name: "Nakul", age: 31},    {name: "Anmol", age: 21}, ];Array:const names = ["Karan", "Joy", "Siddarth", "Ameesh"];We are required to write a JavaScript function that takes in two such arrays and filters the first array in place to contain only those objects whose name property is included in the second array.Therefore, let’s write the code for this function −ExampleThe code for this ... Read More

Converting two arrays into a JSON object in JavaScript

Nikhilesh Aleti
Updated on 23-Sep-2022 11:20:13

5K+ Views

Given two arrays and the task is to convert those two arrays into a JSON object. Where the first array elements are keys of the object and the second array elements are values of the object. Input-Output Scenario Let’s look into the scenario of how two arrays will be converted into a JSON object. Consider two arrays are having some elements. Now, we need to convert these two arrays into a JSON object. Array1 = [1, 2, 3, 4]; Array2 = ['A', 'B', 'C', 'D']; Output = {"1":"A", "2":"B", "3":"C", "4":"D"} //JSON object In this article, ... Read More

Replacing zero starting with whitespace in JavaScript

AmitDiwan
Updated on 22-Oct-2020 13:09:22

407 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. Make sure the prior spaces in number are retained.For example: If the string value is defined as −"004590808"Then the output should come as −"4590808"ExampleThe code for this will be −const str = ' 004590808'; const replaceWithSpace = str => {    let replaced = '';    const regex = new RegExp(/^\s*0+/);    replaced = str.replace(regex, el => {       const { length } = el;       return ' '.repeat(length);    });    return replaced; }; console.log(replaceWithSpace(str));OutputThe output in the console will be −4590808

Square root function without using Math.sqrt() in JavaScript

AmitDiwan
Updated on 22-Oct-2020 13:08:00

4K+ 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.Therefore, let’s write the code for this function −ExampleThe code for this will be −const square = (n, i, j) => {    let mid = (i + j) / 2;    let mul = mid * mid;    if ((mul === n) || (Math.abs(mul - n) < 0.00001)){       return mid;    }else if (mul < n){       return square(n, mid, j);    }else{       return square(n, i, mid);    } ... Read More

Mapping string to Numerals in JavaScript

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

786 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

13K+ 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

143 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 ]

Advertisements