
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 8591 Articles for Front End Technology
Creating an array using a string which contains the key and the value of the properties - JavaScript

97 Views
Suppose, we have a special kind of string like this −const str ="Integer, 1 Float, 2.0Boolean, True Integer, 6Float, 3.66 Boolean, False";We are required to write a JavaScript function that converts the above string into the following array, using the String.prototype.split() method −const arr = [ { "Integer":1, "Float":2.0 }, { "Boolean":true, "Integer":6 }, { "Float":3.66, "Boolean":false } ];We have to use the following rules for conversion −--- marks the end of an object --- ... Read More

1K+ Views
We are required to write a JavaScript function that takes in an array of String/Number literals as the first argument and a String/Number as the second argument.If the variable taken as the second argument is not present in the array, we should return -1.Else if the number is present in the array, then we have to return the index of position the number would have occupied if the array were reversed. We have to do so without actually reversing the array.Then at last we have to attach this function to the Array.prototype object.For example −[45, 74, 34, 32, 23, 65].reversedIndexOf(23); ... Read More

4K+ Views
Extracting unique values from an array means finding and getting only the different elements from a given array, eliminating duplicates. An array is a data structure that stores the same type of data in a linear form. Sometimes it contains duplicate items also. To extract unique values from an array in JavaScript, you can do it by using the set object, the filter() method, the reduce() method, and the includes() method. In this article, we will filter out unique values and return an array containing unique elements. Extract Unique Values from an Array Extracting unique values from an array ... Read More

1K+ Views
We are required to write a JavaScript function that takes in an array of numbers.The function should return the sum of all the prime numbers present in the array.Let’s say the following is our array −const arr = [43, 6, 6, 5, 54, 81, 71, 56, 8, 877, 4, 4];The function should sum the prime numbers i.e.43 + 5 + 71 + 877 = 996ExampleFollowing is the code −const arr = [43, 6, 6, 5, 54, 81, 71, 56, 8, 877, 4, 4]; const isPrime = n => { if (n===1){ return false; }else if(n ... Read More

892 Views
We are required to write a JavaScript function that takes in a string that may contain some backward slashes. The function should return a new string with all the backward slashes replaced with forward slashes.Let’s say the following is our string −const str = 'Th/s str/ng /conta/ns some/ forward slas/hes';ExampleFollowing is the code −const str = 'Th/s str/ng /conta/ns some/ forward slas/hes'; const invertSlashes = str => { let res = ''; for(let i = 0; i < str.length; i++){ if(str[i] !== '/'){ res += str[i]; continue; ... Read More

526 Views
We are required to write a JavaScript function that takes in a string and checks whether it is sorted or not.For example −isSorted('adefgjmxz') // true isSorted('zxmfdba') // true isSorted('dsfdsfva') // falseExampleFollowing is the code −const str = 'abdfhlmxz'; const findDiff = (a, b) => a.charCodeAt(0) - b.charCodeAt(0); const isStringSorted = (str = '') => { if(str.length < 2){ return true; }; let res = '' for(let i = 0; i < str.length-1; i++){ if(findDiff(str[i+1], str[i]) > 0){ res += 'u'; ... Read More

217 Views
Suppose, we have an array of objects like this −const array = [ {key: 'a', value: false}, {key: 'a', value: 100}, {key: 'a', value: null}, {key: 'a', value: 23} ];We are required to write a JavaScript function that takes in one such array and places all the objects that have falsy values for the "value" property to the bottom and sorts all other objects in decreasing order by the "value" property.ExampleFollowing is the code −const arr = [ {key: 'a', value: false}, {key: 'a', value: 100}, {key: 'a', value: null}, {key: 'a', ... Read More

514 Views
We are required to write a JavaScript function that takes in a string which contains English alphabet, for example −const str = 'This is a sample string, will be used to collect some data';The function should return an object containing the count of vowels and consonants in the string i.e. the output should be −{ vowels: 17, consonants: 29 }ExampleFollowing is the code −const str = 'This is a sample string, will be used to collect some data'; const countAlpha = str => { return str.split('').reduce((acc, val) => { const legend = 'aeiou'; ... Read More

785 Views
We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return an array of all the elements from the input array that are greater than or equal to the number taken as the second argument.ExampleFollowing is the code −const arr = [56, 34, 2, 7, 76, 4, 45, 3, 3, 34, 23, 2, 56, 5]; const threshold = 40; const findGreater = (arr, num) => { const res = []; for(let i = 0; i < arr.length; i++){ ... Read More

6K+ Views
Suppose, we have two arrays −const keys = [0, 4, 2, 3, 1]; const values = ["first", "second", "third", "fourth", "fifth"];We are required to write a JavaScript function that takes in the keys and the values array and maps the values to the corresponding keys. The output should be −const map = { 0 => 'first', 4 => 'second', 2 => 'third', 3 => 'fourth', 1 => 'fifth' };ExampleFollowing is the code −const keys = [0, 4, 2, 3, 1]; const values = ["first", "second", "third", "fourth", "fifth"]; const buildMap = (keys, values) => { ... Read More