Sum of Floors Covered by an Elevator in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:20:06

146 Views

ProblemWe are required to write a JavaScript function that takes in an array that represents the floor numbers at which a building lift stopped during an interval of time.From that data, our function should return the count of total number of floors covered by the lift in that time.ExampleFollowing is the code − Live Democonst arr = [7, 1, 7, 1]; const floorsCovered = (arr = []) => {    let res = 0;    for (let i = 0; i < arr.length; i++){       if (arr[i] > arr[i+1]){          res += arr[i] - arr[i+1];   ... Read More

Decrypt Source Message from Code Using Algorithm in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:18:39

173 Views

ProblemWe are required to write a JavaScript function that takes in a decrypted message and returns its source message.All we know is the algorithm used to encrypt that message.And the algorithm is −Reverse the message string.Replace every letter with its ASCII code in quotes (A to '65', h to '104' and so on).Insert digits and spaces as is.ExampleFollowing is the code − Live Democonst str = '12 hello world 30'; const decryptString = (str = '') => {    const alpha = 'abcdefghijklmnopqrstuvwxyz';    let res = '';    for(let i = str.length - 1; i >= 0; i--){     ... Read More

Sum Array of Rational Numbers in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:17:05

187 Views

ProblemWe are required to write a JavaScript function that takes in an array of exactly two subarrays with two numbers each.Both the subarrays represent a rational number in fractional form. Our function should add the rational numbers and return a new array of two numbers representing the simplest form of the added rational number.ExampleFollowing is the code − Live Democonst arr = [    [1, 2],    [1, 3] ]; const findSum = (arr = []) => {    const hcf = (a, b) => b ? hcf(b, a % b) : a;    if(!arr.length){       return null;   ... Read More

Retrieve N Smallest Numbers from an Array in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:15:19

398 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers arr, and a number n.Our function should retrieve the n smallest from the array arr without disturbing their relative order. It means they should not be arranged in increasing or decreasing order rather they should hold their original order.ExampleFollowing is the code − Live Democonst arr = [6, 3, 4, 1, 2]; const num = 3; const smallestInOrder = (arr = [], num) => {    if(arr.length < num){       return arr;    };    const copy = arr.slice();    copy.sort((a, b) => a ... Read More

Replacing Vowels with Their 1-Based Index in a String in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:13:22

615 Views

ProblemWe are required to write a JavaScript function that takes in a string and replaces all occurrences of the vowels in the string with their index in the string (1-based).It means if the second letter of the string is a vowel, it should be replaced by 2.ExampleFollowing is the code − Live Democonst str = 'cancotainsomevowels'; const replaceVowels = (str = '') => {    const vowels = 'aeiou';    let res = '';    for(let i = 0; i < str.length; i++){       const el = str[i];       if(vowels.includes(el)){            res += ... Read More

Can Part of a String Be Rearranged to Form Another String in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:11:38

311 Views

ProblemWe are required to write a JavaScript function that takes in two strings, str1 and str2. Our function should return true if a portion of str1 characters can be rearranged to match str2, otherwise returns false.ExampleFollowing is the code − Live Democonst str1 = 'rkqodlw'; const str2 = 'world'; const canForm = (str1 = '', str2 = '') => {    if(str1.length < str2.length){       return false;    };    const res = str2.split('');    str1.split("").forEach(val => {       if(res.includes(val)){          res.splice(res.indexOf(val), 1);       };    });    return res.length === 0; }; console.log(canForm(str1, str2));OutputFollowing is the console output −true

Determine Century from Year in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:10:04

300 Views

ProblemWe are required to write a JavaScript function that takes in a number that represents a year and find the century that year falls in.For instance,1864 falls in the 19th century.2021 falls in the 21st century.ExampleFollowing is the code − Live Democonst year = 1956; const findCentury = (year) => {    let century = 0;    for(let i = 0; i < year; i++){       if(i % 100 === 0){          century++;       };    };    return century; }; console.log(findCentury(year));OutputFollowing is the console output −20

Sorting Words by Last Character in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:08:06

598 Views

ProblemWe are required to write a JavaScript function that takes in a string of words str. Our function needs to return an array of the words, sorted alphabetically by the final character in each.If two words have the same last letter, the returned array should show them in the order they appeared in the given string.ExampleFollowing is the code − Live Democonst str = 'this is some sample string'; const sortByLast = (str = '') => {    const arr = str.split(' ');    const sorter = (a, b) => {         return a[a.length - 1].charCodeAt(0) - b[b.length ... Read More

Difference Between Numbers and String Numbers in an Array in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:06:43

371 Views

ProblemWe are required to write a JavaScript function that takes in a mixed array of number and string representations of integers.Our function should add up okthe string integers and subtract this from the total of the non-string integers.ExampleFollowing is the code − Live Democonst arr = [5, 2, '4', '7', '4', 2, 7, 9]; const integerDifference = (arr = []) => {    let res = 0;    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(typeof el === 'number'){          res += el;       }else if(typeof el === 'string' && +el){          res -= (+el);       };    };    return res; }; console.log(integerDifference(arr));OutputFollowing is the console output −10

Encode Decimal to Factorial and Back in JavaScript

AmitDiwan
Updated on 19-Apr-2021 08:04:44

347 Views

ProblemCoding decimal numbers with factorials is a way of writing out numbers in a base system that depends on factorials, rather than powers of numbers.In this system, the last digit is always 0 and is in base 0!. The digit before that is either 0 or 1 and is in base 1!. The digit before that is either 0, 1, or 2 and is in base 2!, etc. More generally, the nth-to-last digit is always 0, 1, 2, ..., n and is in base n!.We will need two functions. The first one will receive a decimal number and return a ... Read More

Advertisements