- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 8862 Articles for Front End Technology
306 Views
We are required to write a JavaScript function that takes in a nested array of literals as the only argument. The function should construct a new array that contains all the literal elements present in the input array but without nesting.For example −If the input array is −const arr = [ 1, 3, [5, 6, [7, [6, 5], 4], 3], [4] ];Then the output array should be −const output = [1, 3, 5, 6, 7, 6, 5, 4, 3, 4];ExampleFollowing is the code −const arr = [ 1, 3, [5, 6, [7, [6, 5], 4], 3], [4] ]; ... Read More
153 Views
We are required to write a JavaScript function that takes in an array of Numbers as the only argument. The function should calculate the sum of all numbers in the array and the product of all numbers. Then the function should return the absolute difference between the sum and the product.ExampleFollowing is the code −const arr = [1, 4, 1, 2, 1, 6, 3]; const sumProductDifference = (arr = []) => { const creds = arr.reduce((acc, val) => { let { sum, product } = acc; sum += val; product *= val; return { sum, product }; }, { sum: 0, product: 1 }); const { sum, product } = creds; return Math.abs(sum - product); }; console.log(sumProductDifference(arr));OutputFollowing is the output on console −126
140 Views
We are required to write a JavaScript function that takes in an array of sorted numbers. The function should calculate the mean and the mode of the dataset. Then if the mean and mode are equal, the function should return true, false otherwise.For example −If the input array is −const arr = [5, 3, 3, 3, 1];Then the output for this array should be true because both the mean and median of this array are 3.ExampleFollowing is the code −const arr = [5, 3, 3, 3, 1]; mean = arr => (arr.reduce((a, b) => a + b))/(arr.length); mode = arr ... Read More
361 Views
We are required to write a JavaScript function that takes in a string as the only argument. The function should then iterate through the string and find and return the longest word from the string.For example −If the input string is −const str = 'Coding in JavaScript is really fun';Then the output string should be −const output = 'JavaScript';ExampleFollowing is the code −const str = 'Coding in JavaScript is really fun'; const findLongest = (str = '') => { const strArr = str.split(' '); const word = strArr.reduce((acc, val) => { let { length: len ... Read More
2K+ Views
We are required to write a JavaScript function that takes in a string as the only argument. The function should construct a new reversed string based on the input string using the for a loop.ExampleFollowing is the code −const str = 'this is the original string'; const reverseString = (str = '') => { let reverse = ''; const { length: len } = str; for(let i = len - 1; i >= 0; i--){ reverse += str[i]; }; return reverse; }; console.log(reverseString(str));OutputFollowing is the output on console −gnirts lanigiro eht si siht
837 Views
We are required to write a JavaScript function that takes in a string that might contain some opening and closing brackets. The function should whether for all the opening brackets there exists a closing bracket or not. If the brackets are rightly matched, the function should return true, false otherwise.For example −f('(hello (world))') = true f('(hello (world)') = falseExampleFollowing is the code −const str1 = '(hello (world))'; const str2 = '(hello (world)'; const validateBrackets = (str = '') => { const strArr = str.split(''); let counter = 0; for (let i = 0, len = strArr.length; i ... Read More
1K+ Views
We are required to write a JavaScript function that takes in two strings let's call them str1 and str2.The size of str1 is guaranteed to be greater than that of str2. We are required to find the smallest substring in str1 that contains all the characters contained in str2.For example −If the input strings are −const str1 = 'abcdefgh'; const str2 = 'gedcf';Then the output should be −const output = 'cdefg';because this the smallest consecutive substring of str1 that contains all characters of str2.ExampleFollowing is the code −const str1 = 'abcdefgh'; const str2 = 'gedcf'; const subIncludesAll = (str, str2) ... Read More
121 Views
We are required to write a JavaScript function that takes in a string as the first argument and a separator character as the second argument.The first string is guaranteed to be a camelCased string. The function should convert the case of the string by separating the words by the separator provided as the second argument.For example −If the input string is −const str = 'thisIsAString'; const separator = '_';Then the output string should be −const output = 'this_is_a_string';ExampleFollowing is the code −const str = 'thisIsAString'; const separator = '_'; const separateCase = (str = '', separator = ' ') => ... Read More
453 Views
We are required to write a JavaScript function that takes in a string as the only argument.The string might contain both uppercase and lowercase alphabets.The function should construct a new string based on the input string in which all the uppercase letters are converted to lowercase and all the lowercase letters are converted to uppercase.ExampleFollowing is the code −const str = 'ThIs Is A STriNG'; const findLetter = (char = '') => { if(char.toLowerCase() === char.toUpperCase){ return char; }else if(char.toLowerCase() === char){ return char.toUpperCase(); }else{ return char.toLowerCase(); ... Read More
3K+ Views
It is a common practice that when websites display anyone's private email address they often mask it in order to maintain privacy.Therefore for example −If someone's email address is −const email = 'ramkumar@example.com';Then it is displayed like this −const masked = 'r...r@example.com';We are required to write a JavaScript function that takes in an email string and returns the masked email for that string.ExampleFollowing is the code −const email = 'ramkumar@example.com'; const maskEmail = (email = '') => { const [name, domain] = email.split('@'); const { length: len } = name; const maskedName = name[0] + '...' + ... Read More