We are required to write a JavaScript function that takes in an array of Numbers. The function should check whether the numbers in the array are in increasing order, or in decreasing order or in no specific order.If the array contains only one element then we should return a message saying not enough elements.And if the array has all the elements equal, we should return a message saying all elements are equal.ExampleThe code for this will be −const arr1 = [7, 2, 1, 3, 2, 1]; const arr2 = [1, 1, 2, 3, 7, 7]; const determineOrder = arr => ... Read More
We are required to write a JavaScript function that takes in a number and recursively adds the digits of the number until the result is not a single digit number.For example, If the number is −54563Then the output should be 5, because,= 5 + 4 + 5 + 6 + 3 = 23 = 2 + 3 = 5ExampleThe code for this will be −const num = 54563; const addRecursively = num => { if(num < 10){ return num; }; let sum = 0; while(num !== 0) { sum += (num%10); num = parseInt(num/10); }; return addRecursively(sum); }; console.log(addRecursively(num));OutputThe output in the console −3
Suppose we have an array of arrays like this −const arr = [ [ ['juice', 'apple'], ['maker', 'motts'], ['price', 12] ], [ ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11] ] ];We are required to write a JavaScript function that takes in one such array and returns a new array of objects built based on the input array.So, for the above array, the output should look like this −const output = [ {juice: 'apple', maker: 'motts', price: 12}, {juice: 'orange', maker: 'sunkist', price: 11} ];ExampleThe code for this will be ... Read More
We are required to write a JavaScript function that takes in an array of decimal strings. The function should return an array of strings of integers obtained by flooring the original corresponding decimal values of the array.For example, If the input array is −const input = ["1.00","-2.5","5.33333","8.984563"];Then the output should be −const output = ["1","-2","5","8"];ExampleThe code for this will be −const input = ["1.00","-2.5","5.33333","8.984563"]; const roundIntegers = arr => { const res = []; arr.forEach((el, ind) => { const strNum = String(el); res[ind] = parseInt(strNum); }); return res; }; console.log(roundIntegers(input));OutputThe output in the console −[ 1, -2, 5, 8 ]
Suppose, we have an array of objects like this −const arr = [ {"name": "toto", "uuid": 1111}, {"name": "tata", "uuid": 2222}, {"name": "titi", "uuid": 1111} ];We are required to write a JavaScript function that splits the objects into separate array of arrays that have the similar values for the uuid property.OutputTherefore, the output should look like this −const output = [ [ {"name": "toto", "uuid": 1111}, {"name": "titi", "uuid": 1111} ], [ {"name": "tata", "uuid": 2222} ] ];The code for this will be −const ... Read More
Suppose we have an array of sorted numbers but some elements of the array are out of their sorted order.We are required to write a JavaScript function that takes in one such array and returns a subarray of all those elements that are out of order.ExampleThe code for this will be −const arr = ["2", "3", "7", "4", "5", "6", "1"]; const findOutOfOrder = arr => { let notInOrder = []; notInOrder = arr.filter((el, ind) => { return ind && this.next !== +el || (this.next = +el + 1, false); }, { next: null }); return notInOrder; }; console.log(findOutOfOrder(arr));OutputThe output in the console −[ '7', '1' ]
Snake case is basically a style of writing strings by replacing the spaces with '_' and converting the first letter of each word to lowercase.We are required to write a JavaScript function that takes in a string and converts it to snake case.ExampleThe code for this will be −const str = 'This is a simple sentence'; const toSnakeCase = (str = '') => { const strArr = str.split(' '); const snakeArr = strArr.reduce((acc, val) => { return acc.concat(val.toLowerCase()); }, []); return snakeArr.join('_'); }; console.log(toSnakeCase(str));OutputFollowing is the output on console −this_is_a_simple_sentence
We are required to write a JavaScript function that takes in a string and returns the index of first character that appears twice in the string.If there is no such character then we should return -1.ExampleThe code for this will be −const str = 'Hello world, how are you'; const firstRepeating = str => { const map = new Map(); for(let i = 0; i < str.length; i++){ if(map.has(str[i])){ return map.get(str[i]); }; map.set(str[i], i); }; return -1; }; console.log(firstRepeating(str));OutputFollowing is the output on console −2
We are required to write a JavaScript function that takes in a string and reverses the words in the string that have an odd number of characters in them.Any substring in the string qualifies to be a word, if either it is encapsulated by two spaces on either ends or present at the end or beginning and followed or preceded by a space.ExampleThe code for this will be −const str = 'hello world, how are you'; const idOdd = str => str.length % 2 === 1; const reverseOddWords = (str = '') => { const strArr = str.split(' '); ... Read More
Suppose, we have an array of numbers like this −const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1];We are required to write a JavaScript function that takes in one such array and counts the sum of all distinct elements of the array.For example:The output for the array mentioned above will be −30ExampleThe code for this will be −const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1]; const distinctSum = arr => { let res = 0; for(let i = 0; i < arr.length; i++){ if(i === arr.lastIndexOf(arr[i])){ res += arr[i]; }; continue; }; return res; }; console.log(distinctSum(arr));OutputFollowing is the output on console −30