Transform Array of Numbers to Array of Alphabets Using JavaScript

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

284 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

277 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

Encrypt Censored Words Using JavaScript

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

295 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

Convert Numbers and Operands to Words in JavaScript

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

189 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

Closest Pair to Kth Index Element in Tuple Using Python

AmitDiwan
Updated on 17-Apr-2021 12:56:44

372 Views

When it is required to find the closest pair to the Kth index element in a tuple, the ‘enumerate’ method can be used along with ‘abs’ method.Below is the demonstration of the same −Example Live Demomy_list = [(5, 6), (66, 76), (21, 35), (90, 8), (9, 0)] print("The list is : ") print(my_list) my_tuple = (17, 23) print("The tuple is ") print(my_tuple) K = 2 print("The value of K has been initialized to ") print(K) min_diff, my_result = 999999999, None for idx, val in enumerate(my_list):    diff = abs(my_tuple[K - 1] - val[K - 1])    if diff ... Read More

Create List of Tuples with Number and Its Cube Using Python

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

908 Views

When it is required to create a list from a given list that have a number and its cube, the list comprehension can be used.Below is the demonstration of the same −Example Live Demomy_list = [32, 54, 47, 89] print("The list is ") print(my_list) my_result = [(val, pow(val, 3)) for val in my_list] print("The result is ") print(my_result)OutputThe list is [32, 54, 47, 89] The result is [(32, 32768), (54, 157464), (47, 103823), (89, 704969)]ExplanationA list is defined, and is displayed on the console.The list comprehension is used to iterate through the list, and use the ‘pow’ method to get ... Read More

Maximum and Minimum K Elements in Tuple Using Python

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

1K+ Views

When it is required to find the maximum and minimum K elements in a tuple, the ‘sorted’ method is used to sort the elements, and enumerate over them, and get the first and last elements.Below is the demonstration of the same −Example Live Demomy_tuple = (7, 25, 36, 9, 6, 8) print("The tuple is : ") print(my_tuple) K = 2 print("The value of K has been initialized to ") print(K) my_result = [] my_tuple = list(my_tuple) temp = sorted(my_tuple) for idx, val in enumerate(temp):    if idx < K or idx >= len(temp) - K:       ... Read More

Find the Size of a Tuple in Python

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

376 Views

When it is required to find the size of a tuple, the ‘sizeof’ method can be used.Below is the demonstration of the same −Example Live Demoimport sys tuple_1 = ("A", 1, "B", 2, "C", 3) tuple_2 = ("Java", "Lee", "Code", "Mark", "John") tuple_3 = ((1, "Bill"), ( 2, "Ant"), (3, "Fox"), (4, "Cheetah")) print("The first tuple is :") print(tuple_1) print("The second tuple is :") print(tuple_2) print("The third tuple is :") print(tuple_3) print("Size of first tuple is : " + str(sys.getsizeof(tuple_1)) + " bytes") print("Size of second tuple is : " + str(sys.getsizeof(tuple_2)) + " bytes") print("Size of third tuple is: ... Read More

Sending Personalised Messages to User Using JavaScript

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

262 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

597 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

Advertisements