Found 6683 Articles for Javascript

Writing a custom URL shortener function in JavaScript

AmitDiwan
Updated on 20-Apr-2021 07:03:07

2K+ Views

ProblemWe are required to write two JavaScript functions −First function should take in a long url and return a short url that corresponds to it.The second function should take in the short url and redirect to the original url.ExampleFollowing is the code − Live Democonst url = 'https://www.google.com/search?client=firefox-b-d&q=google+search'; const obj = {}; const urlShortener = (longURL = '') => {    let shortURL = "short.ly/" + longURL.replace(/[^a-z]/g, '').slice(-4);    if(!obj[shortURL]){       obj[shortURL] = longURL;    };    return shortURL;    }    const urlRedirector = (shortURL = '') => {    return obj[shortURL]; }; const short = urlShortener(url); const ... Read More

Rectifying the time string in JavaScript

AmitDiwan
Updated on 20-Apr-2021 06:56:18

59 Views

ProblemWe are required to write a JavaScript function that takes in a time string in “HH:MM:SS” format.But there was a problem in addition, so many of the time strings are broken which means the MM part may exceed 60, and SS part may as well exceed 60.Our function should make required changes to the string and return the new rectified string.For instance −"08:11:71" -> "08:12:11"ExampleFollowing is the code − Live Democonst str = '08:11:71'; const rectifyTime = (str = '') => {    if(!Boolean(str)){       return str;    };    const re = /^(\d\d):(\d\d):(\d\d)$/;    if (!re.test(str)){     ... Read More

Generating the sequence of first n look and say numbers in JavaScript

AmitDiwan
Updated on 20-Apr-2021 06:59:29

338 Views

ProblemIn mathematics, the look-and-say sequence is the sequence of integers beginning as follows −1, 11, 21, 1211, 111221, 312211, …To generate a member of the sequence from the previous member, we read off the digits of the previous member, counting the number of digits in groups of the same digit.For instance, the next number to 1211 is −111221Because if we read the digit of 1211 louder it will be −One one, one two, two one which gives us 111221We are required to write a JavaScript function that takes in a number n and returns the first n terms of look ... Read More

Counting the number of IP addresses present between two IP addresses in JavaScript

AmitDiwan
Updated on 20-Apr-2021 06:41:40

994 Views

ProblemWe are required to write a JavaScript function that takes in two IPv4 addresses, and returns the number of addresses between them (including the first one, excluding the last one).This can be done by converting them to decimal and finding their absolute difference.ExampleFollowing is the code − Live Democonst ip1 = '20.0.0.10'; const ip2 = '20.0.1.0'; const countIp = (ip1, ip2) => {    let diff = 0;    const aIp1 = ip1.split(".");    const aIp2 = ip2.split(".");    if (aIp1.length !== 4 || aIp2.length !== 4) {       return "Invalid IPs: incorrect format";    }    for (x ... Read More

Converting an unsigned 32 bit decimal to corresponding ipv4 address in JavaScript

AmitDiwan
Updated on 20-Apr-2021 06:37:34

801 Views

ProblemConsider the following ipv4 address −128.32.10.1If we convert it to binary, the equivalent will be −10000000.00100000.00001010.00000001And further if we convert this binary to unsigned 32 bit decimal, the decimal will be −2149583361Hence, we can say that the ipv4 equivalent of 2149583361 is 128.32.10.1We are required to write a JavaScript function that takes in a 32-bit unsigned integer and returns its equivalent ipv4 address.ExampleFollowing is the code − Live Democonst num = 2149583361; const int32ToIp = (num) => {    return (num >>> 24 & 0xFF) + '.' +    (num >>> 16 & 0xFF) + '.' +    (num >>> 8 & 0xFF) + '.' +    (num & 0xFF); }; console.log(int32ToIp(num));OutputFollowing is the console output −128.32.10.1

Converting decimal to binary or hex based on a condition in JavaScript

AmitDiwan
Updated on 20-Apr-2021 06:34:14

154 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should convert the number to binary or hex based on −If a number is even, convert it to binary.If a number is odd, convert it to hex.ExampleFollowing is the code − Live Democonst num = 1457; const conditionalConvert = (num = 1) => {    const isEven = num % 2 === 0;    const toBinary = () => num.toString(2);    const toHexadecimal = () => num.toString(16);    return isEven       ? toBinary()       : toHexadecimal(); }; console.log(conditionalConvert(num));OutputFollowing is the console output −5b1

Finding the k-prime numbers with a specific distance in a range in JavaScript

AmitDiwan
Updated on 20-Apr-2021 06:30:49

285 Views

K-Prime NumbersA natural number is called k-prime if it has exactly k prime factors, counted with multiplicity.Which means even though the only prime factor of 4 is 2 it will be a 2-prime number because −4 = 2 * 2 and both 2s will be counted separately taking the count to 2.Similarly, 8 is 3-prime because 8 = 2 * 2 * 2 taking the count to 3.ProblemWe are required to write a JavaScript function that takes in a number k, a distance and a range.Our function should return an array of arrays containing k-prime numbers within the range the ... Read More

Sorting numbers in ascending order and strings in alphabetical order in an array in JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 09:42:03

1K+ Views

In the realm of JavaScript programming, the ability to sort numbers in ascending order and strings in alphabetical order within an array holds paramount importance. Efficiently arranging elements in such a manner not only enhances the organization and readability of data but also plays a pivotal role in various data manipulation and analysis tasks. By harnessing the power of JavaScript's built-in sorting capabilities, developers can effortlessly arrange array elements in the desired order. In this article, we will explore the intricacies of sorting numbers in ascending order and strings in alphabetical order within an array, unveiling the underlying mechanisms and ... Read More

Mapping array of numbers to an object with corresponding char codes in JavaScript

AmitDiwan
Updated on 20-Apr-2021 06:24:47

534 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. For each number in the array, we need to create an object. The object key will be the number, as a string. And the value will be the corresponding character code, as a string.We should finally return an array of the resulting objects.ExampleFollowing is the code − Live Democonst arr = [67, 84, 98, 112, 56, 71, 82]; const mapToCharCodes = (arr = []) => {    const res = [];    for(let i = 0; i < arr.length; i++){       const el = ... Read More

Mean of an array rounded down to nearest integer in JavaScript

AmitDiwan
Updated on 20-Apr-2021 06:22:24

303 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function is supposed to return the average of the given array rounded down to its nearest integer.ExampleFollowing is the code − Live Democonst arr = [45, 23, 67, 68, 12, 56, 99]; const roundedMean = (arr = []) => {    const { sum, count } = arr.reduce((acc, val) => {       let { sum, count } = acc;       count++;       sum += val;       return { sum, count };    }, {       sum: 0, count: 0    });    const mean = sum / (count || 1);    return Math.round(mean); }; console.log(roundedMean(arr));OutputFollowing is the console output −53

Advertisements