Find Two Missing Numbers in JavaScript

AmitDiwan
Updated on 20-Apr-2021 07:22:59

181 Views

ProblemWe are required to write a JavaScript function that takes in an array in which all the numbers appear thrice except one which appears twice and one which appears only one. Our function should find and return these two numbers.ExampleFollowing is the code − Live Democonst arr = [1, 1, 1, 2, 2, 3]; const findMissing = (arr = []) => {    let x = 0;    let y = 0;    for(let i = 0; i < arr.length; i++){       if(arr.filter(a => a === arr[i]).length === 2){          y = arr[i];       };       if(arr.filter(b => b === arr[i]).length === 1){          x = arr[i];       };    };    return [x, y]; }; console.log(findMissing(arr));OutputFollowing is the console output −[3, 2]

Projectile Class to Calculate Height and Distance in JavaScript

AmitDiwan
Updated on 20-Apr-2021 07:18:23

167 Views

ProblemWe are required to write a JavaScript class, Projectile, which takes in 3 arguments when initialized −starting height (0 ≤ h0 < 200)starting velocity (0 < v0 < 200)angle of the projectile when it is released (0° < a < 90°, measured in degrees).We need to write the following method for the Projectile class.A horiz method, which also takes in an argument t and calculates the horizontal distance that the projectile has traveled. [takes in a double, returns a double]ExampleThe code for this class will be − Live Democlass Projectile{    constructor(h, u, ang){       this.h = h;   ... Read More

Cutting Off Number at Each Digit to Construct an Array in JavaScript

AmitDiwan
Updated on 20-Apr-2021 07:06:13

121 Views

ProblemWe are required to write a JavaScript function that takes in a number. Our function should return an array of strings containing the number cut off at each digit.ExampleFollowing is the code − Live Democonst num = 246; const cutOffEach = (num = 1) => {    const str = String(num);    const res = [];    let temp = '';    for(let i = 0; i < str.length; i++){       const el = str[i];       temp += el;       res.push(temp);    };    return res; }; console.log(cutOffEach(num));OutputFollowing is the console output −[ '2', '24', '246' ]

Create 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

Generate Sequence of First N Look-and-Say Numbers in JavaScript

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

422 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

Rectifying Time String in JavaScript

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

109 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

Count IP Addresses Between Two IPs in JavaScript

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

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

Convert Unsigned 32-bit Decimal to IPv4 Address in JavaScript

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

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

Convert Decimal to Binary or Hex in JavaScript Based on Condition

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

209 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 K Prime Numbers with Specific Distance in a Range in JavaScript

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

418 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

Advertisements