Found 10710 Articles for Web Development

Encrypting a string based on an algorithm using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:08:03

620 Views

ProblemWe are required to write a JavaScript function that takes in a string and encrypts it based on the following algorithm −The string contains only space separated words.We need to encrypt each word in the string using the following rules−The first letter needs to be converted to its ASCII code.The second letter needs to be switched with the last letter.Therefore, according to this, the string ‘good’ will be encrypted as ‘103doo’.ExampleFollowing is the code − Live Democonst str = 'good'; const encyptString = (str = '') => {    const [first, second] = str.split('');    const last = str[str.length - 1]; ... Read More

Encrypting a string based on an algorithm in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:25:34

175 Views

ProblemWe are required to write a JavaScript function that takes in a string and encrypts it based on the following algorithm −The string contains only space separated words.We need to encrypt each word in the string using the following rules −The first letter needs to be converted to its ASCII code.The second letter needs to be switched with the last letter.Therefore, according to this, the string ‘good’ will be encrypted as ‘103doo’.ExampleFollowing is the code − Live Democonst str = 'good'; const encyptString = (str = '') => {    const [first, second] = str.split('');    const last = str[str.length - ... Read More

Finding the first non-consecutive number in an array in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:23:41

298 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function should return that first element from the array which is not the natural successor of its previous element.It means we should return that element which is not +1 its previous element given that there exists at least one such element in the array.ExampleFollowing is the code − Live Democonst arr = [1, 2, 3, 4, 6, 7, 8]; const findFirstNonConsecutive = (arr = []) => {    for(let i = 0; i < arr.length - 1; i++){       const el = arr[i]; ... Read More

Checking if decimals share at least two common 1 bits in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:21:38

67 Views

ProblemWe are required to write a JavaScript function that takes in two numbers. Our function should return true if the numbers have 1 in the binary representation at the same index twice, false otherwise.ExampleFollowing is the code − Live Democonst num1 = 10; const num2 = 15; const checkBits = (num1 = 1, num2 = 1) => {    let c = num1.toString(2).split('');    let d = num2.toString(2).split('');    if(c.length > d.length){       c = c.slice(c.length - d.length);    }else{       d = d.slice(d.length - c.length);    };    let count = 0;    for(let i = ... Read More

Limiting string up to a specified length in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:18:30

879 Views

ProblemWe are required to write a JavaScript function that takes in a string and a number. Our function should return the truncated version of the given string up to the given limit followed by "..." if the result is shorter than the original string otherwise our function should return the same string if nothing was truncated.ExampleFollowing is the code − Live Democonst str = 'Testing String'; const num = 8; const limitString = (str = '', num = 1) => {    const { length: len } = str;    if(num < len){       return str.slice(0, num) + '...';    }else{       return str;    }; }; console.log(limitString(str, num));OutputFollowing is the console output −Testing ...

Constructing a string based on character matrix and number array in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:16:16

139 Views

ProblemWe are required to write a JavaScript function that takes in an n * n matrix of string characters and an array of integers (positive and unique).Our function should construct a string of those characters whose 1-based index is present in the array of numbers.Character Matrix −[    [‘a’, ‘b’, ‘c’, d’],    [‘o’, ‘f’, ‘r’, ‘g’],    [‘h’, ‘i’, ‘e’, ‘j’],    [‘k’, ‘l’, ‘m’, n’] ];Number Array −[1, 4, 5, 7, 11]Should return ‘adore’ because these are the characters present at the 1-based indices specified by number array in the matrix.ExampleFollowing is the code − Live Democonst arr = ... Read More

Returning a range or a number that specifies the square root of a number in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:12:09

128 Views

ProblemWe are required to write a JavaScript function that takes in an integer n and returns either −an integer k if n is a square number, such that k * k == n ora range (k, k+1), such that k * k < n and n < (k+1) * (k+1).ExampleFollowing is the code − Live Democonst num = 83; const squareRootRange = (num = 1) => {    const exact = Math.sqrt(num);    if(exact === Math.floor(exact)){       return exact;    }else{         return [Math.floor(exact), Math.ceil(exact)];    }; }; console.log(squareRootRange(num));OutputFollowing is the console output −[9, 10]

Returning a decimal that have 1s in its binary form only at indices specified by array in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:10:39

62 Views

ProblemWe are required to write a JavaScript function that takes in an array of unique non-negative integers. Our function should return a 32-bit integer such that the integer, in its binary representation, has 1 at only those indexes (counted from right) which are in the sequence.ExampleFollowing is the code − Live Democonst arr = [1, 2, 0, 4]; const buildDecimal = (arr = []) => {    const bitArr = Array(31).fill(0);    let res = 0;    arr.forEach(el => {       bitArr.splice((31 - el), 1, 1);    })    bitArr.forEach((bit, index) => {       res += (2 * (31-index) * bit);    });    return res; }; console.log(buildDecimal(arr));OutputFollowing is the console output −14

Finding the 1-based index score of a lowercase alpha string in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:07:12

126 Views

ProblemWe are required to write a JavaScript function that takes in a lowercase alphabet string. The index of ‘a’ in alphabets is 1, of ‘b’ is 2 ‘c’ is 3 … of ‘z’ is 26.Our function should sum all the index of the string characters and return the result.ExampleFollowing is the code − Live Democonst str = 'lowercasestring'; const findScore = (str = '') => {    const alpha = 'abcdefghijklmnopqrstuvwxyz';    let score = 0;    for(let i = 0; i < str.length; i++){       const el = str[i];       const index = alpha.indexOf(el);       score += (index + 1);    };    return score; }; console.log(findScore(str));OutputFollowing is the console output −188

Finding the only even or the only odd number in a string of space separated numbers in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:04:48

356 Views

ProblemWe are required to write a JavaScript function that takes in a string that contains numbers separated by spaces.The string either contains all odd numbers and only one even number or all even numbers and only one odd number. Our function should return that one different number from the string.ExampleFollowing is the code − Live Democonst str = '2 4 7 8 10'; const findDifferent = (str = '') => {    const odds = [];    const evens = [];    const arr = str    .split(' ')    .map(Number);    arr.forEach(num => {       if(num % 2 ... Read More

Advertisements