Finding Number of Open Water Taps After N Chances Using JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:21:58

122 Views

ProblemSuppose a school organises this game on their Annual Day celebration −There are ”n” water taps and “n” students are chosen at random. The instructor asks the first student to go to every tap and open it. Then he has the second student go to every second tap and close it. The third goes to every third tap and, if it is closed, he opens it, and if it is open, he closes it. The fourth student does this to every fourth tap, and so on. After the process is completed with the "n"th student, how many taps are open?We ... Read More

Sort Array with Weights Using JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:21:39

400 Views

ProblemWe are required to write a JavaScript function that takes in an array of string that contains weights with three units: grams (G), kilo-grams (KG) and tonnes (T). Our function should sort the array into order of weight from light to heavy.We are required to write a JavaScript function that takes in an array of string that contains weights with three units: grams (G), kilo-grams (KG) and tonnes (T). Our function should sort the array into order of weight from light to heavy.ExampleFollowing is the code − Live Democonst arr = ['1456G', '1KG', '.5T', '.005T', '78645G', '23KG']; const arrangeWeights = (arr ... Read More

Move All Vowels to the End of String Using JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:21:22

364 Views

ProblemWe are required to write a JavaScript function that takes in a string. Our function should construct a new string in which all the consonants should hold their relative position and all the vowels should be pushed to the end of string.ExampleFollowing is the code − Live Democonst str = 'sample string'; const moveVowels = (str = '') => {    const vowels = 'aeiou';    let front = '';    let rear = '';    for(let i = 0; i < str.length; i++){       const el = str[i];       if(vowels.includes(el)){          rear += el;       }else{          front += el;       };    };    return front + rear; }; console.log(moveVowels(str));Outputsmpl strngaei

Sorting According to Number of 1s in Binary Representation Using JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:21:06

201 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function should sort the numbers according to decreasing number of 1s present in the binary representation of those numbers and return the new array.ExampleFollowing is the code − Live Democonst arr = [5, 78, 11, 128, 124, 68, 6]; const countOnes = (str = '') => {    let count = 0;    for(let i = 0; i < str.length; i++){       const el = str[i];       if(el === '1'){          count++;       };    };    return count; }; const sortByHighBit = (arr = []) => {    arr.sort((a, b) => countOnes(b) - countOnes(a));    return arr; }; console.log(sortByHighBit(arr));Output[ 5, 78, 11, 128, 124, 68, 6 ]

Unique Pairs in Array That Form Palindrome Words in JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:18:04

173 Views

ProblemWe are required to write a JavaScript function that takes in an array of unique words.Our function should return an array of all such index pairs, the words at which, when combined yield a palindrome word.ExampleFollowing is the code − Live Democonst arr = ["abcd", "dcba", "lls", "s", "sssll"]; const findPairs = (arr = []) => {    const res = [];    for ( let i = 0; i < arr.length; i++ ){       for ( let j = 0; j < arr.length; j++ ){          if (i !== j ) {             let k = `${arr[i]}${arr[j]}`;             let l = [...k].reverse().join('');             if (k === l)             res.push( [i, j] );          }       };    };    return res; }; console.log(findPairs(arr));Output[ [ 0, 1 ], [ 1, 0 ], [ 2, 4 ], [ 3, 2 ] ]

Convert Numbers to Corresponding Alphabets and Characters Using JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:16:09

582 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers in string format. Our function must return a string. The numbers correspond to the letters of the alphabet in reverse order: a=26, z=1 etc.We should also account for '!', '?' and ' ' that are represented by '27', '28' and '29' respectively.ExampleFollowing is the code − Live Democonst arr = ['5', '23', '2', '1', '13', '18', '6']; const convertToString = (arr) => {    let res = '';    for (let char of arr) {       if (Number(char)

Convert Characters to ASCII Codes in JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:15:53

379 Views

ProblemWe are required to write a JavaScript function that takes in a string. Our function should turn each character into its ASCII character code and join them together to create a number. Then we should replace all instances of 7 from this number to 1 to construct another number. Finally, we should return the difference of both these numbersExampleFollowing is the code − Live Democonst str = 'AVEHDKDDS'; const ASCIIDifference = (str = '') => {    return str    .split('')    .map(c => c.charCodeAt(0))    .join('')    .split('')    .map(Number)    .filter(str => str === 7)    .length * 6; }; console.log(ASCIIDifference(str));Output12

Squared and Square Rooted Sum of Numbers in JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:15:18

428 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function should take each number in the array and square it if it is even, or square root the number if it is odd and then return the sum of all the new numbers rounded to two decimal places.ExampleFollowing is the code − Live Democonst arr = [45, 2, 13, 5, 14, 1, 20]; const squareAndRootSum = (arr = []) => {    const res = arr.map(el => {       if(el % 2 === 0){          return el * el;       }else{          return Math.sqrt(el);       };    });    const sum = res.reduce((acc, val) => acc + val);    return sum; }; console.log(squareAndRootSum(arr));Output613.5498231854631

Finding Next Prime Number Using JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:14:52

1K+ Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should that smallest number which is just greater than n and is a prime number.ExampleFollowing is the code − Live Democonst num = 101; const isPrime = (num) => {    let sqrtnum = Math.floor(Math.sqrt(num));    let prime = num !== 1;    for(let i = 2; i < sqrtnum + 1; i++){       if(num % i === 0){          prime = false;          break;       };    };    return prime; } const nextPrime = (num = 1) => {    while(!isPrime(++num)){    };    return num; }; console.log(nextPrime(num));Output103

Solve Maximum Subarray Problem Using Divide and Conquer in Python

AmitDiwan
Updated on 19-Apr-2021 11:13:29

432 Views

When it is required solve the maximum subarray problem using the divide and conquer method, Below is the demonstration of the same −Example Live Demodef max_crossing_sum(my_array, low, mid, high):    sum_elements = 0    sum_left_elements = -10000    for i in range(mid, low-1, -1):    sum_elements = sum_elements + my_array[i]    if (sum_elements > sum_left_elements):       sum_left_elements = sum_elements    sum_elements = 0    sum_right_elements = -1000    for i in range(mid + 1, high + 1):       sum_elements = sum_elements + my_array[i]       if (sum_elements > sum_right_elements):     ... Read More

Advertisements