Found 6710 Articles for Javascript

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

AmitDiwan
Updated on 24-Aug-2020 05:41:29

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
Updated on 24-Aug-2020 05:39:37

510 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

JavaScript algorithm for converting Roman numbers to decimal numbers

Disha Verma
Updated on 11-Mar-2025 16:35:09

490 Views

For converting a Roman numeral into a decimal number, JavaScript provides simple and efficient ways. Roman numerals are a number system that started in ancient Rome and are still used today in different situations. Converting Roman numerals to decimal (integer) values can be useful in many applications, such as date conversions, numbering systems, or educational tools. In this article, we will see a JavaScript algorithm to convert Roman numerals into decimal numbers efficiently. Understanding Roman Numerals Roman numerals are a Number System that uses combinations of letters from the Latin alphabet to represent values. It uses seven letters to ... Read More

JavaScript algorithm for converting integers to roman numbers

AmitDiwan
Updated on 21-Aug-2020 15:12:46

1K+ Views

Let’s say, we are required to write a function, say intToRoman(), which, as the name suggests, returns a Roman equivalent of the number passed in it as an argument.Let’s write the code for this function −Exampleconst intToRoman = (num) => {    let result = "";    while(num){       if(num>=1000){          result += "M";          num -= 1000;       }else if(num>=500){          if(num>=900){             result += "CM";             num -= 900;          }else{   ... Read More

How to check existence of NaN keyword in an array JavaScript

AmitDiwan
Updated on 21-Aug-2020 15:08:01

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
Updated on 21-Aug-2020 15:05:26

137 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

Convert number to reversed array of digits JavaScript

AmitDiwan
Updated on 21-Aug-2020 15:03:12

281 Views

Let’s say, we have to write a function that takes in a number and returns an array of numbers with elements as the digits of the number but in reverse order. We will convert the number into a string, then split it to get an array of strings of digit, then we will convert the string into numbers, reverse the array and finally return it.Following is our function that takes in a number to be reversed −const reversifyNumber = (num) => {    const numString = String(num);    return numString.split("").map(el => {       return +el;    }).reverse(); };Exampleconst ... Read More

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

AmitDiwan
Updated on 21-Aug-2020 15:01:44

313 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 “-” JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:56:26

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
Updated on 21-Aug-2020 14:53:41

365 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

Advertisements