Found 10483 Articles for Web Development

Encrypting censored words using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:57:55

283 Views

ProblemWe are required to write a JavaScript function that takes in a string. Our function should convert the string according to following rules −The words should be Caps, Every word should end with '!!!!', Any letter 'a' or 'A' should become '@', Any other vowel should become '*'.ExampleFollowing is the code − Live Democonst str = 'ban censored words'; const maskWords = (str = '') => {    let arr=str.split(' ');    const res=[]    for (let i=0; i

Numbers and operands to words in JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:57:30

178 Views

ProblemWe are required to write a JavaScript function that takes in a string of some mathematical operation and return its literal wording.ExampleFollowing is the code −const str = '5 - 8'; const convertToWords = (str = '') => {    const o = {       "+" : "Plus",       "-" : "Minus",       "*" : "Times",       "/" : "Divided By",       "**" : "To The Power Of",       "=" : "Equals",       "!=" : "Does Not Equal",    }    const n = {   ... Read More

Sending personalised messages to user using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:54:04

255 Views

ProblemWe are required to write a JavaScript function that takes in two strings. The first string specifies the user name and second the owner name.If the user and owner are the same our function should return ‘hello master’, otherwise our function should return ‘hello’ appended with the name of that user.ExampleFollowing is the code − Live Democonst name = 'arnav'; const owner = 'vijay'; function greet (name, owner) {    if (name === owner){       return 'Hello master';    }    return `Hello ${name}`; }; console.log(greet(name, owner));OutputHello arnav

Third smallest number in an array using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:49:26

588 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers of length at least three.Our function should simply return the third smallest number from the array.ExampleFollowing is the code − Live Democonst arr = [6, 7, 3, 8, 2, 9, 4, 5]; const thirdSmallest = () => {    const copy = arr.slice();    for(let i = 0; i < 2; i++){       const minIndex = copy.indexOf(Math.min(...copy));       copy.splice(minIndex, 1);    };    return Math.min(...copy); }; console.log(thirdSmallest(arr));Output4

Removing letters to make adjacent pairs different using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:49:04

155 Views

ProblemWe are required to write a JavaScript function that takes in a string that contains only ‘A’, ‘B’ and ‘C’. Our function should find the minimum number of characters needed to be removed from the string so that the characters in each pair of adjacent characters are different.ExampleFollowing is the code − Live Democonst str = "ABBABCCABAA"; const removeLetters = (str = '') => {    const arr = str.split('')    let count = 0    for (let i = 0; i < arr.length; i++) {       if (arr[i] === arr[i + 1]) {          count += 1          arr.splice(i, 1)          i -= 1       }    }    return count } console.log(removeLetters(str));Output3

Counting rings in letters using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:48:45

198 Views

ProblemWe are required to write a JavaScript function that takes in a string of English alphabets.Our function should count the number of rings present in the string.O', 'b', 'p', 'e', 'A', etc. all have one rings whereas 'B' has 2ExampleFollowing is the code − Live Democonst str = 'some random text string'; function countRings(str){    const rings = ['A', 'D', 'O', 'P', 'Q', 'R', 'a', 'b', 'd', 'e', 'g', 'o', 'p', 'q'];    const twoRings = ['B'];    let score = 0;    str.split('').map(x => rings.includes(x)    ? score++    : twoRings.includes(x)    ? score = score + 2    : x    );    return score; } console.log(countRings(str));Output7

Rearranging digits to form the greatest number using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:47:38

720 Views

ProblemWe are required to write a JavaScript function that takes in one positive three-digit integer and rearranges its digits to get the maximum possible number.ExampleFollowing is the code − Live Democonst num = 149; const maxRedigit = function(num) {    if(num < 100 || num > 999)       return null    return +num    .toString()    .split('')    .sort((a, b) => b - a)    .join('') }; console.log(maxRedigit(num));Output941

Longest string consisting of n consecutive strings in JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:46:12

396 Views

ProblemWe are required to write a JavaScript function that takes in an array of strings. Our function should create combinations by combining all possible n consecutive strings in the array and return the longest such string that comes first.ExampleFollowing is the code − Live Democonst arr = ["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"]; const num = 2; function longestConsec(strarr, k) {    if (strarr.length == 0 || k > strarr.length || k longStr.length ){          longStr = newStr.join('');       }    }    return longStr; } console.log(longestConsec(arr, num));Outputabigailtheta

Can all array elements mesh together in JavaScript?

AmitDiwan
Updated on 17-Apr-2021 12:16:09

138 Views

ProblemTwo words can mesh together if the ending substring of the first is the starting substring of the second. For instance, robinhood and hoodie can mesh together.We are required to write a JavaScript function that takes in an array of strings. If all the words in the given array mesh together, then our function should return the meshed letters in a string, otherwise we should return an empty string.ExampleFollowing is the code − Live Democonst arr = ["allow", "lowering", "ringmaster", "terror"]; const meshArray = (arr = []) => {    let res = "";    for(let i = 0; i < ... Read More

Largest index difference with an increasing value in JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:12:20

115 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr. Our function should return the largest difference in indexes j - i such that arr[i] {    const { length: len } = arr;    let res = 0;    for(let i = 0; i < len; i++){       for(let j = i + 1; j < len; j++){          if(arr[i] res){             res = j - i;          };       };    };    return res; }; console.log(findLargestDifference(arr));OutputAnd the output in the console will be −3

Advertisements