- 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 6686 Articles for Javascript
234 Views
We are required to write a JavaScript function that takes in a lowercase string and sorts it in the reverse order i.e., b should come before a, c before b and so on.For example:If the input string is −const str = "hello";Then the output should be −const output = "ollhe";The code for this will be −const string = 'hello'; const sorter = (a, b) => { const legend = [-1, 0, 1]; return legend[+(a < b)]; } const reverseSort = str => { const strArr = str.split(""); return strArr .sort(sorter) .join(""); }; console.log(reverseSort(string));Following is the output on console −ollhe
487 Views
We have an array of numbers like this −const arr = [1, 1, 5, 2, -4, 6, 10];We are required to write a function that returns a new array, of the same size but with each element being the sum of all elements until that point.Therefore, the output should look like −const output = [1, 2, 7, 9, 5, 11, 21];Therefore, let’s write the function partialSum(), The full code for this function will be −const arr = [1, 1, 5, 2, -4, 6, 10]; const partialSum = (arr) => { const output = []; arr.forEach((num, index) => { ... Read More
1K+ Views
We are required to create a function that takes in a number and finds the sum of its digits recursively until the sum is a one-digit number.For examplefindSum(12345) = 1+2+3+4+5 = 15 = 1+5 = 6Therefore, the output should be 6.Let’s write the code for this function findSum()// using recursion const findSum = (num) => { if(num < 10){ return num; } const lastDigit = num % 10; const remainingNum = Math.floor(num / 10); return findSum(lastDigit + findSum(remainingNum)); } console.log(findSum(2568));We check if the number is less than 10, it’s already minified and ... Read More
68 Views
We are required to write a JavaScript function that takes in an array of literals. Our function should return a new array with all the triplets filtered.The code for this will be −const arr1 = [1, 1, 1, 3, 3, 5]; const arr2 = [1, 1, 1, 1, 3, 3, 5]; const arr3 = [1, 1, 1, 3, 3, 3]; const arr4 = [1, 1, 1, 1, 3, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7, 7]; const removeTriplets = arr => { const hashMap = arr => arr.reduce((acc, val) => { ... Read More
318 Views
We have a parentArray that contains many sub arrays each of the same size, each sub array is an array of objects containing two properties: key and value. Within a subarray it is confirmed that two objects cannot have the same key but all subarrays have the same pair of n keys where n is the size of the sub array.Our job is to prepare an object with key as key of objects and value being an array that contains all the values for that particular key.Here is a sample parent array −const parentArray = [[ { key: 123, ... Read More
1K+ Views
We have an object with other objects being its property value, it is nested to 2-3 levels or even more.Here is the sample object −const people = { Ram: { fullName: 'Ram Kumar', details: { age: 31, isEmployed: true } }, Sourav: { fullName: 'Sourav Singh', details: { age: 22, isEmployed: false } }, Jay: { fullName: 'Jay ... Read More
187 Views
We have an array of objects that contains data about some cars. The array is given as follows −const cars = [{ company: 'Honda', type: 'SUV' }, { company: 'Hyundai', type: 'Sedan' }, { company: 'Suzuki', type: 'Sedan' }, { company: 'Audi', type: 'Coupe' }, { company: 'Tata', type: 'SUV' }, { company: 'Morris Garage', type: 'Hatchback' }, { company: 'Honda', type: 'SUV' }, { company: 'Tata', type: 'Sedan' }, { company: 'Honda', type: 'Hatchback' }];We are required to write a program ... Read More
512 Views
We need to write a function that reads a string and converts the odd indexed characters in the string to upperCase and the even ones to lowerCase and returns a new string.Full code for doing the same will be −const text = 'Hello world, it is so nice to be alive.'; const changeCase = (str) => { const newStr = str .split("") .map((word, index) => { if(index % 2 === 0){ return word.toLowerCase(); }else{ return word.toUpperCase(); } }) ... Read More
2K+ Views
We will be given an array of numbers / strings that contains some duplicate entries, all we have to do is to return the frequency of each element in the array.Returning an object with an element as key and its value as frequency would be perfect for this situation.We will iterate over the array with a forEach() loop and keep increasing the count of elements in the object if it already exists otherwise we will create a new property for that element in the object.And lastly, we will return the object.The full code for this problem will be −const arr ... Read More
1K+ Views
We are required to write a function breakString() that takes in two arguments first the string to be broken and second is a number that represents the threshold count of characters after reaching which we have to repeatedly add line breaks in place of spaces.So, let’s do it. We will iterate over the with a for loop, we will keep a count that how many characters have elapsed with inserting a ‘’ if the count exceeds the limit and we encounter a space we replace it with line break in the new string and reset the count to 0 otherwise ... Read More