Found 8591 Articles for Front End Technology

Checking for coprime numbers in JavaScript

AmitDiwan
Updated on 14-Oct-2020 07:38:56

879 Views

Two numbers are said to be co-primes if there exists no common prime factor amongst them (1 is not a prime number).We are required to write a function that takes in two numbers and returns true if they are coprimes otherwise returns false.ExampleThe code for this will be −const areCoprimes = (num1, num2) => {    const smaller = num1 > num2 ? num1 : num2;    for(let ind = 2; ind < smaller; ind++){       const condition1 = num1 % ind === 0;       const condition2 = num2 % ind === 0;       ... Read More

Finding how many times a specific letter is appearing in a sentence in JavaScript

AmitDiwan
Updated on 14-Oct-2020 07:36:40

478 Views

We are required to write a JavaScript function that finds how many times a specific letter is appearing in the sentence.ExampleThe code for this will be −const string = 'This is just an example string for the program'; const countAppearances = (str, char) => {    let count = 0;    for(let i = 0; i < str.length; i++){       if(str[i] !== char){          // using continue to move to next iteration          continue;       };       // if we reached here it means that str[i] and char ... Read More

Consecutive elements sum array in JavaScript

AmitDiwan
Updated on 14-Oct-2020 07:26:09

511 Views

We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as the sum of two consecutive elements from the original array.For example, if the input array is −const arr1 = [1, 1, 2, 7, 4, 5, 6, 7, 8, 9];Then the output should be −const output = [2, 9, 9, 13, 17]ExampleThe code for this will be −const arr11 = [1, 1, 2, 7, 4, 5, 6, 7, 8, 9]; const consecutiveSum = arr => {    const res = [];    for(let i = 0; i < arr.length; i += 2){       res.push(arr[i] + (arr[i+1] || 0));    };    return res; }; console.log(conseutiveSum(arr1));OutputThe output in the console −[ 2, 9, 9, 13, 17 ]

JavaScript Array: Checking for multiple values

AmitDiwan
Updated on 14-Oct-2020 07:22:36

238 Views

We are required to write a JavaScript function that takes in two arrays of Numbers and checks whether all the elements of the first array exist in the second or not.ExampleThe code for this will be −const arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89]; const multipleIncludes = (first, second) => {    const indexArray = first.map(el => {       return second.indexOf(el);    });    return indexArray.indexOf(-1) === -1; } console.log(multipleIncludes(arr1, arr2));OutputThe output in the console −true

Padding a string with random lowercase alphabets to fill length in JavaScript

AmitDiwan
Updated on 14-Oct-2020 07:20:47

264 Views

We are required to write a function that takes in two arguments, first is a string and second is a number. The length of string is always less than or equal to the number. We have to insert some random lowercase alphabets at the end of the string so that its length becomes exactly equal to the number and we have to return the new string.ExampleLet’s write the code for this function −const padString = (str, len) => {    if(str.length < len){       const random = Math.floor(Math.random() * 26);       const randomAlpha = String.fromCharCode(97 + ... Read More

Pushing NaN to last of array using sort() in JavaScript

AmitDiwan
Updated on 14-Oct-2020 07:18:43

435 Views

We have an array that contains String and number mixed data types, we have to write a sorting function that sorts the array so that the NaN values always end up at the bottom.The array should contain all the valid numbers in the front, followed by string literals, followed by NaN.The code for this will be −const arr = [344, 'gfd', NaN, '', 15, 'f', 176, NaN, 736, NaN, 872, 859, 'string', 13, 'new', NaN, 75]; const sorter = (a, b) => {    if(a !== a){       return 1;    }else if(b !== b){       ... Read More

Pushing positives and negatives to separate arrays in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:51:51

393 Views

We are required to write a function that takes in an array and returns an object with two arrays positive and negative. They both should be containing all positive and negative items respectively from the array.We will be using the Array.prototype.reduce() method to pick desired elements and put them into an object of two arrays.ExampleThe code for this will be −const arr = [97, -108, 13, -12, 133, -887, 32, -15, 33, -77]; const splitArray = (arr) => {    return arr.reduce((acc, val) => {       if(val < 0){          acc['negative'].push(val);       }else{ ... Read More

Find the closest value of an array in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:50:24

241 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a number as the second argument. The function should then return the number from the array which closest to the number given to the function as second argument.ExampleThe code for this will be −const arr = [3, 56, 56, 23, 7, 76, -2, 345, 45, 76, 3]; const num = 37 const findClosest = (arr, num) => {    const creds = arr.reduce((acc, val, ind) => {       let { diff, index } = acc;       ... Read More

JavaScript Array showing the index of the lowest number

AmitDiwan
Updated on 12-Oct-2020 11:48:57

166 Views

We are required to write a JavaScript function that takes in an array of numbers. Then the function should return the index of the smallest number in the array.ExampleThe code for this will be −const arr = [3, 56, 56, 23, 7, 76, -2, 345, 45, 76, 3]; const lowestIndex = arr => {    const creds = arr.reduce((acc, val, ind) => {       let { num, index } = acc;       if(val < num){          num = val;          index = ind;       };       return { num, index };    }, {       num: Infinity,       index: -1    });    return creds.index; }; console.log(lowestIndex(arr));OutputThe output in the console −6

Grouping array values in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:47:14

468 Views

Suppose we have an array of objects containing some years and weeks data like this −const arr = [    {year: 2017, week: 45},    {year: 2017, week: 46},    {year: 2017, week: 47},    {year: 2017, week: 48},    {year: 2017, week: 50},    {year: 2017, week: 52},    {year: 2018, week: 1},    {year: 2018, week: 2},    {year: 2018, week: 5} ];We are required to write a JavaScript function that takes in one such array. The function should return a new array where all the objects that have common value for the "year" property are grouped into ... Read More

Advertisements