Javascript Articles

Page 423 of 534

Sum of consecutive numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 24-Aug-2020 554 Views

Let’s say, we have to write a function that takes in an array and returns another array in which the consecutive similar numbers are added up together.For example −const array = [1, 5, 5, 5, 8, 8, 9, 1, 4, 4, 2];The output should be −[1, 15, 16, 9, 1, 8, 2]All consecutive 5s added up to 15, then 2 consecutive 8s added up to 16 similarly 4s added up to 8.Therefore, let’s write the code for this function. We will use the Array.prototype.reduce() method here to reduce the original array and simultaneously construct a new one.Exampleconst array = [1, ...

Read More

Recursively loop through an array and return number of items with JavaScript?

AmitDiwan
AmitDiwan
Updated on 24-Aug-2020 2K+ Views

We have to write a function, say searchRecursively() that takes in an array and a search query and returns the count of that search query in the nested array.For example, if the array is given by −const names = ["rakesh", ["kalicharan", "krishna", "rakesh", "james", ["michael", "nathan", "rakesh", "george"]]];Then −searchRecursively(names, ‘’rakesh’);Should return 3 because it makes a total of 3 appearances in the array. Therefore, let’s write the code for this recursive function −Exampleconst names = ["rakesh", ["kalicharan", "krishna", "rakesh", "james", ["michael", "nathan", "rakesh", "george"]]]; const searchRecursively = (arr, query, count = 0, len = 0) => {    if(len < ...

Read More

JavaScript program to merge two objects into a single object and adds the values for same keys

AmitDiwan
AmitDiwan
Updated on 24-Aug-2020 550 Views

We have to write a function that takes in two objects, merges them into a single object, and adds the values for same keys. This has to be done in linear time and constant space, means using at most only one loop and merging the properties in the pre-existing objects and not creating any new variable.So, let’s write the code for this function −Exampleconst obj1 = {    value1: 45,    value2: 33,    value3: 41,    value4: 4,    value5: 65,    value6: 5,    value7: 15, }; const obj2 = {    value1: 34,    value3: 71,   ...

Read More

How to check existence of NaN keyword in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Aug-2020 1K+ Views

We have an array of elements that contains both truth and false values. Our job is to write a function that returns an array with indices of those elements which are NaN in the original array.NaN !== NaNThe datatype of NaN is actually number. Although NaN is a falsy value, it has a peculiar property that no other datatype or variable has. It’s that the expression NaN === NaN yields false. And it’s only in the case of NaN that its false.So, we can use this behavior to our good and pick out NaN value index. The code for this ...

Read More

Order items alphabetically apart from certain words JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Aug-2020 165 Views

Let’s say, we have two arrays both containing String literals, one of which is required to sort alphabetically, but if this array, the one we have to sort contains some words from the other array, those words should appear at the very top and the rest of the element should be sorted alphabetically.Let’s write a function, say excludeSorting(arr, ex) where arr is the array to be sorted and ex is the array of strings that should appear at top in arr (if they appear in arr).Exampleconst arr = ['apple', 'cat', 'zebra', 'umbrella', 'disco', 'ball', 'lemon', 'kite', 'jack', 'nathan']; const toBeExcluded ...

Read More

Generate colors between #CCCCCC and #3B5998 for a color meter with JavaScript?

AmitDiwan
AmitDiwan
Updated on 21-Aug-2020 345 Views

We have to write a function that generates a random color between two given colors. Let’s tackle this problem in parts −First → We write a function that generates a random number between two given numbers.Second → Instead of using the hex scale for random color generation, we will map the hex to 0 to 15 decimal scale and use that instead.Lastly → We loop over any of the given color strings and generate a random color.Exampleconst randomBetween = (a, b) => {    const max = Math.max(a, b);    const min = Math.min(a, b);    return Math.floor(Math.random() * (max ...

Read More

How to Replace null with &ldquo;-&rdquo; JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Aug-2020 2K+ Views

We have to write a function that takes in an object with many keys and replaces all false values with a dash (‘ - ’). We will simply iterate over the original object, checking for the keys that contain false values, and we will replace those false values with ‘-’ without consuming any extra space (i.e., in place)Exampleconst obj = {    key1: 'Hello',    key2: 'World',    key3: '',    key4: 45,    key5: 'can i use arrays',    key6: null,    key7: 'fast n furious',    key8: undefined,    key9: '',    key10: NaN, }; const swapValue = ...

Read More

Pad a string using random numbers to a fixed length using JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Aug-2020 408 Views

We have to write a function, say padSting() 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 numbers at the end of the string so that its length becomes exactly equal to the number and we have to return the new string.Therefore, let’s write the code for this function −Exampleconst padString = (str, len) => {    if(str.length < len){       const random = Math.floor(Math.random() * 10);       return padString(str + random, ...

Read More

Sort a JavaScript array so the NaN values always end up at the bottom.

AmitDiwan
AmitDiwan
Updated on 21-Aug-2020 637 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 normal numbers first followed by string literals and then followed by NaN numbers.We know that the data type of NaN is “number”, so we can’t check for NaN like !number && !string. Moreover, if we simply check the tautology and falsity of elements then empty strings will also satisfy the same condition which NaN or undefined satisfies.Check for NaNSo how ...

Read More

Get unique item from two different array in JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Aug-2020 209 Views

We are required to make a function that accepts an array of arrays, and returns a new array with all elements present in the original array of arrays but remove the duplicate items.For example − If the input is −const arr = [    [12, 45, 65, 76, 76, 87, 98],    [54, 65, 98, 23, 78, 9, 1, 3],    [87, 98, 3, 2, 123, 877, 22, 5, 23, 67] ];Then the output should be single array of unique elements like this −[    12, 45, 54, 78, 9,    1, 2, 123, 877, 22,    5, 67 ]Exampleconst arr = [    [12, 45, 65, 76, 76, 87, 98],    [54, 65, 98, 23, 78, 9, 1, 3],    [87, 98, 3, 2, 123, 877, 22, 5, 23, 67] ]; const getUnique = (arr) => {    const newArray = [];    arr.forEach((el) => newArray.push(...el));    return newArray.filter((item, index) => {       return newArray.indexOf(item) === newArray.lastIndexOf(item);    }); }; console.log(getUnique(arr));OutputThe output in the console will be −[    12, 45, 54, 78, 9,    1, 2, 123, 877, 22,    5, 67 ]

Read More
Showing 4221–4230 of 5,338 articles
« Prev 1 421 422 423 424 425 534 Next »
Advertisements