Capitalize Each Word in an Array Using a Recursive Function in JavaScript

AmitDiwan
Updated on 23-Nov-2020 11:27:17

418 Views

We are required to write a JavaScript function that takes in an array of String literals. The function should do the following two things −Make use of recursive approachMake first word of each string element capital.Our function should do this without using extra space for storing another array.For example −If the input array is −const arr = ['apple', 'banana', 'orange', 'grapes'];Then the array should be transformed to −const output = ['Apple', 'Banana', 'Orange', 'Grapes'];ExampleThe code for this will be −const arr = ['apple', 'banana', 'orange', 'grapes']; const capitalize = (arr = [], ind = 0) => {    const helper ... Read More

Store Two Arrays as Key-Value Pair in One Object in JavaScript

AmitDiwan
Updated on 23-Nov-2020 11:26:07

736 Views

Suppose, we have two arrays of literals of same length like these −const arr1 = ['firstName', 'lastName', 'age', 'address', 'isEmployed']; const arr2 = ['Rahul', 'Sharma', 23, 'Tilak Nagar', false];We are required to write a JavaScript function that takes in two such arrays.The function should construct an object mapping the elements of the second array to the corresponding elements of the first array.We will use the Array.prototype.reduce() method to iterate over the arrays, building the object.ExampleThe code for this will be −const arr1 = ['firstName', 'lastName', 'age', 'address', 'isEmployed']; const arr2 = ['Rahul', 'Sharma', 23, 'Tilak Nagar', false]; const mapArrays = ... Read More

Make Vowels Uppercase and Change Letters in JavaScript

AmitDiwan
Updated on 23-Nov-2020 11:24:56

519 Views

We are required to write a JavaScript function that takes in a string as the only input.The function should construct a new string based on the input string in which all the vowels should be uppercased and change each alphabet to the corresponding next alphabet.For example − If the input string is −const str = 'newString';Therefore, the output for the above input should look like this −const output = 'oExSusIoh';ExampleThe code for this will be −const str = 'newString'; const capitiliseAndMove = (str = '') => {    let s = '';    s = str.replace(/[a−z]/g, function(c) {     ... Read More

Reversing the Alphabet in JavaScript

AmitDiwan
Updated on 23-Nov-2020 11:23:26

264 Views

We are required to write a JavaScript function that takes in a single alphabet as the only input. The function should compute the position of the that alphabet from starting and return an alphabet that’s at the same position but from the back.ExampleThe code for this will be −const alpha = 'g'; const findCounterPart = (alpha = '') => {    let alphabet = 'abcdefghijklmnopqrstuvwxyz';    let firstpart = alphabet.substring(0, 13).split('');    let secondpart = alphabet.substring(13).split('').reverse();    let solution = '';    if (firstpart.indexOf(alpha) !== −1) {       solution = secondpart[firstpart.indexOf(alpha)];    } else {       ... Read More

Possible Combinations and Convert into Alphabet Algorithm in JavaScript

AmitDiwan
Updated on 23-Nov-2020 11:21:52

232 Views

Suppose we are given the mapping a = 1, b = 2, ... z = 26, and an encoded message. We are required to write a JavaScript function that takes in the message.The function should count the number of ways it can be decoded.For example, the message '111' would give 3, since it could be decoded as 'aaa, 'ka', and 'ak'.ExampleThe code for this will be −const waysToProcess = ( message, ways = 0 ) => {    if ( message.length ) {       ways = waysToProcess( message.slice( 1 ,message.length), ways );       const numCurr = ... Read More

Expressive Words Problem Case in JavaScript

AmitDiwan
Updated on 23-Nov-2020 11:20:37

208 Views

Sometimes people repeat letters to represent extra feeling, such as "hello" −> "heeellooo", "hi" −> "hiiii". In these strings like "heeellooo", we have groups of adjacent letters that are all the same: "h", "eee", "ll", "ooo".For some given string S, a query word is stretchy if it can be made to be equal to S by any number of applications of the following extension operation: choose a group consisting of characters c, and add some number of characters c to the group so that the size of the group is 3 or more.For example, starting with "hello", we could do ... Read More

Indexing Numbers to Alphabets in JavaScript

AmitDiwan
Updated on 23-Nov-2020 11:19:27

2K+ Views

We are required to write a JavaScript function that takes in a number between the range [0, 25], both inclusive.Return valueThe function should return the corresponding alphabets for that number.ExampleThe code for this will be −const num = 15; const indexToAlpha = (num = 1) => {    // ASCII value of first character    const A = 'A'.charCodeAt(0);    let numberToCharacter = number => {       return String.fromCharCode(A + number);    };    return numberToCharacter(num); }; console.log(indexToAlpha(num));OutputAnd the output in the console will be −P

Sorting Elements of Stack Using JavaScript

AmitDiwan
Updated on 23-Nov-2020 11:18:14

283 Views

We are required to write a JavaScript function that takes in an array of Integers. Making use of recursion and the push and pop methods of the array, the function should sort the array inplace.ExampleThe code for this will be −const stack = [−3, 14, 18, −5, 30]; const sortStack = (stack = []) => {    if (stack.length > 0) {       let t = stack.pop();       sortStack(stack);       sortedInsert(stack, t);    }; } const sortedInsert = (stack, e) => {    if (stack.length == 0 || e > stack[stack.length − 1]) {       stack.push(e);    } else {       let x = stack.pop();       sortedInsert(stack, e);       stack.push(x);    } } sortStack(stack); console.log(stack);OutputAnd the output in the console will be −[ −5, −3, 14, 18, 30 ]

Prefix Calculator Using Stack in JavaScript

AmitDiwan
Updated on 23-Nov-2020 11:16:59

1K+ Views

We are required to make a calculator with the RPN (reverse polish notation) input method using Stacks in JavaScript.Consider the following input array −const arr = [1, 5, '+', 6, 3, '-', '/', 7, '*'];Process −1 is an operand, push to Stack.5 is an operand, push to Stack.'+' is an operator, pop 1 and 5, calculate them and push result to Stack.6 is an operand, push to Stack.3 is an operand, push to Stack.'−' is an operator, pop 6 and 3, subtract them and push result to Stack.'/' is an operator, pop 6 and 3, divided them and push result ... Read More

Implementing Heap Sort Using Vanilla JavaScript

AmitDiwan
Updated on 23-Nov-2020 11:15:20

232 Views

Heap Sort is basically a comparison-based sorting algorithm. It can be thought of as an improved selection sort − like that algorithm, it divides its input into a sorted and an unsorted region, and it interactively shrinks the unsorted region by extracting the target (largest or smallest) element and moving that to the sorted region.ExampleThe code for this will be −const constructHeap = (arr, ind) => {    let left = 2 * ind + 1;    let right = 2 * ind + 2;    let max = ind;    if (left < len && arr[left] > arr[max]) { ... Read More

Advertisements