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
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
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
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
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
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
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
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
ProblemWe are required to write a JavaScript function that takes in a string number of at least five digits. Our function should return the greatest sequence of five consecutive digits found within the number given.ExampleFollowing is the code − Live Democonst num = '123546544'; const findGreatestFiveDigit = (num = '') => { const str = num.toString(); const arr = []; for(let i = 0; i < str.length; i++){ arr.push(str.slice(i, i + 5)); }; return Math.max(...arr); }; console.log(findGreatestFiveDigit(num));Output54654
ProblemWe are required to write a JavaScript function that takes in a number n(n>0). Our function should return an array that contains the continuous parts of odd or even digits. It means that we should split the number at positions when we encounter different numbers (odd for even, even for odd).ExampleFollowing is the code − Live Democonst num = 124579; const splitDifferent = (num = 1) => { const str = String(num); const res = []; let temp = ''; for(let i = 0; i < str.length; i++){ const el = str[i]; ... Read More