Found 6683 Articles for Javascript

Transforming array of numbers to array of alphabets using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:00:00

158 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function should return a string made of four parts −a four character 'word', made up of the characters derived from the first two and last two numbers in the array. order should be as read left to right (first, second, second last, last), the same as above, post sorting the array into ascending order, the same as above, post sorting the array into descending order, the same as above, post converting the array into ASCII characters and sorting alphabetically.The four parts should form a ... Read More

Moving vowels and consonants using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:58:15

186 Views

ProblemWe are required to write a JavaScript function that takes in a string of English alphabets. Our function should construct a new string and every consonant should be pushed forward 9 places through the alphabet. If they pass 'z', start again at 'a'. And every vowel should be pushed by 5 places.ExampleFollowing is the code − Live Democonst str = 'sample string'; const moveWords = (str = '') => {    str = str.toLowerCase();    const legend = 'abcdefghijklmnopqrstuvwxyz';    const isVowel = char => 'aeiou'.includes(char);    const isAlpha = char => legend.includes(char);    let res = '';    for(let i ... Read More

Encrypting censored words using JavaScript

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

204 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

118 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

202 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

491 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

101 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

130 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

564 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

306 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

Advertisements