Javascript Articles

Page 252 of 534

Converting number of corresponding string without using library function in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Apr-2021 381 Views

ProblemWe are required to write a JavaScript function that takes in a number n and converts it to the corresponding string without using the inbuilt functions String() or toString() or using string concatenation.ExampleFollowing is the code − Live Democonst num = 235456; const convertToString = (num) => {    let res = '';    while(num){       res = (num % 10) + res;       num = Math.floor(num / 10);    };    return res; }; console.log(convertToString(num));OutputFollowing is the console output −235456

Read More

Checking for a Doubleton Number in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Apr-2021 339 Views

Doubleton NumberWe will call a natural number a "doubleton number" if it contains exactly two distinct digits. For example, 23, 35, 100, 12121 are doubleton numbers, and 123 and 9980 are not.ProblemWe are required to write a JavaScript function that takes in a number and return true if it is a doubleton number, false otherwise.ExampleFollowing is the code − Live Democonst num = 121212; const isDoubleTon = (num = 1) => {    const str = String(num);    const map = {};    for(let i = 0; i < str.length; i++){       const el = str[i];       ...

Read More

Converting to hex and summing the numeral part in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Apr-2021 308 Views

ProblemWe are required to write a JavaScript function that takes in a string. Our function should convert every character of the string to the hex value of its ascii code, then the result should be the sum of the numbers in the hex strings ignoring the letters present in hex.ExampleFollowing is the code − Live Democonst str = "Hello, World!"; const toHexAndSum = (str = '') => {    return str    .split('')    .map(c=>c.charCodeAt())    .map(n=>n.toString(16))    .join('')    .split('')    .filter(c=>'123456789'.includes(c))    .map(d=>parseInt(d))    .reduce((a, b)=>a+b, 0) }; console.log(toHexAndSum(str));OutputFollowing is the console output −91

Read More

Finding the nth element of the lucas number sequence in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Apr-2021 302 Views

Lucas NumbersLucas numbers are numbers in a sequence defined like this −L(0) = 2 L(1) = 1 L(n) = L(n-1) + L(n-2)ProblemWe are required to write a JavaScript function that takes in a number n and return the nth lucas number.ExampleFollowing is the code − Live Democonst num = 21; const lucas = (num = 1) => {    if (num === 0)       return 2;    if (num === 1)       return 1;    return lucas(num - 1) +       lucas(num - 2); }; console.log(lucas(num));OutputFollowing is the console output −24476

Read More

Finding intersection of arrays of intervals in JavaScript

AmitDiwan
AmitDiwan
Updated on 09-Apr-2021 557 Views

ProblemJavaScript function that takes in two arrays, arr1 and arr2 of intervals which are pairwise disjoint and in sorted order.A closed interval [a, b] (with a

Read More

Array thirds with equal sums in JavaScript

AmitDiwan
AmitDiwan
Updated on 09-Apr-2021 162 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers as the first and the only argument. Our function should return true if and only if we can partition the array into three non-empty parts with equal sums, false otherwise.For example, if the input to the function is −const arr = [3, 3, 6, 5, -2, 2, 5, 1, -9, 4];Then the output should be −const output = true;Output ExplanationBecause, 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4ExampleThe code for this will be − Live ...

Read More

Maximum consecutive 1s after n swaps in JavaScript

AmitDiwan
AmitDiwan
Updated on 09-Apr-2021 217 Views

ProblemWe are required to write a JavaScript function that takes in a binary arr (array that contains only 0 or 1), arr, as the first argument, and a number, num, as the second argument.We can change at most num 0s present in the array to 1s, and our function should return the length of the longest (contiguous) subarray that contains only 1s after making these changes.For example, if the input to the function is −const arr = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0]; const num = 2;Then the output should be −const output = 6;Output ...

Read More

Commons including duplicates in array elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 09-Apr-2021 150 Views

ProblemWe are required to write a JavaScript function that takes in an array of strings, arr, as the first and the only argument.Our function is supposed to return an array of all characters that show up in all strings within the array arr (including duplicates).For example, if a character occurs 2 times in all strings but not 3 times, we need to include that character 2 times in the final answer.For example, if the input to the function is −const arr = ['door', 'floor', 'crook'];Then the output should be −const output = ['r', 'o', 'o'];ExampleThe code for this will be ...

Read More

Problem: Time taken by tomatoes to rot in JavaScript

AmitDiwan
AmitDiwan
Updated on 09-Apr-2021 201 Views

ProblemWe are required to write a JavaScript function that takes in a 2-D array of numbers, arr, as the only argument.The numbers in the array can be −the value 0 which represents an empty cell;the value 1 which represents a fresh tomato;the value 2 which represents a rotten tomato.Every minute, any fresh tomato that is adjacent (4-directionally) to a rotten tomato becomes rotten.Our function is supposed to return the minimum number of minutes that must elapse until no cell has a fresh tomato. If this is impossible, we should return -1 instead.For example, if the input to the function is ...

Read More

Parts of array with n different elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 09-Apr-2021 203 Views

ProblemWe are required to write a JavaScript function that takes in an array of literals, arr, as the first argument. The second argument to our function will be a number, num. Our function should count and return the number of subarrays of the array that contains exactly num distinct elements.For example, if the input to the function is −const arr = [12, 15, 12, 15, 18]; const num = 2;Then the output should be −const output = 7;Output ExplanationSubarrays formed with exactly 2 different elements −[12, 15], [15, 12], [12, 15], [15, 18], [12, 15, 12], [15, 12, 15], [12, ...

Read More
Showing 2511–2520 of 5,338 articles
« Prev 1 250 251 252 253 254 534 Next »
Advertisements