Found 6686 Articles for Javascript

Sum of all prime numbers in an array - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:32:29

843 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

Replace backward slashes with forward slashes - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:31:09

733 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

Check if a string is sorted in JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:29:59

408 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

Sorting an array object by property having falsy value - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:28:40

143 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

Counting occurrences of vowel, consonants - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:27:25

413 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

Getting equal or greater than number from the list of numbers in JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:25:50

669 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

Building a Map from 2 arrays of values and keys in JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:24:32

5K+ 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

Remove array duplicates by property - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:23:13

156 Views

Suppose, we have an array of objects like this −const arr = [{name: "Jack", age: "14"}, {name: "bob", age: "14"}, {name: "sue", age: "21"}, {name: "Jill", age: "16"}, {name: "Jack", age: "21"}];We are required to write a JavaScript function that takes in one such array and removes all the objects that have duplicate values for the name.Therefore, for the above array, the output should be −const arr = [{name: "Jack", age: "14"}, {name: "bob", age: "14"}, {name: "sue", age: "21"}, {name: "Jill", age: "16"}];ExampleFollowing is the code −const arr = [    {name: "Jack", age: "14"},    {name: "bob", age: ... Read More

JavaScript - Convert an array to key value pair

AmitDiwan
Updated on 30-Sep-2020 13:21:57

6K+ Views

Suppose we have an array like this −const arr = [ {"name": "Rahul", "score": 89},    {"name": "Vivek", "score": 88},    {"name": "Rakesh", "score": 75},    {"name": "Sourav", "score": 82},    {"name": "Gautam", "score": 91},    {"name": "Sunil", "score": 79}, ];We are required to write a JavaScript function that takes in one such array and constructs an object where name value is the key and the score value is their value.Use the Array.prototype.reduce() method to construct an object from the array.ExampleFollowing is the code −const arr = [    {"name": "Rahul", "score": 89},    {"name": "Vivek", "score": 88},    {"name": ... Read More

JavaScript - How to pick random elements from an array?

AmitDiwan
Updated on 30-Sep-2020 13:20:15

2K+ Views

Suppose, we have an array of literals that contains no duplicate elements like this −const arr = [2, 5, 4, 45, 32, 46, 78, 87, 98, 56, 23, 12];We are required to write a JavaScript function that takes in an array of unique literals and a number n.The function should return an array of n elements all chosen randomly from the input array and no element should appear more than once in the output array.ExampleFollowing is the code −const arr = [2, 5, 4, 45, 32, 46, 78, 87, 98, 56, 23, 12]; const chooseRandom = (arr, num = 1) ... Read More

Advertisements