Javascript Articles - Page 259 of 534

Reverse alphabetically sorted strings in JavaScript

AmitDiwan
Updated on 10-Oct-2020 07:29:45

348 Views

We are required to write a JavaScript function that takes in a lowercase string and sorts it in the reverse order i.e., b should come before a, c before b and so on.For example:If the input string is −const str = "hello";Then the output should be −const output = "ollhe";The code for this will be −const string = 'hello'; const sorter = (a, b) => {    const legend = [-1, 0, 1];    return legend[+(a < b)]; } const reverseSort = str => {    const strArr = str.split("");    return strArr    .sort(sorter)    .join(""); }; console.log(reverseSort(string));Following is the output on console −ollhe

Converting array of Numbers to cumulative sum array in JavaScript

AmitDiwan
Updated on 10-Oct-2020 07:28:15

622 Views

We have an array of numbers like this −const arr = [1, 1, 5, 2, -4, 6, 10];We are required to write a function that returns a new array, of the same size but with each element being the sum of all elements until that point.Therefore, the output should look like −const output = [1, 2, 7, 9, 5, 11, 21];Therefore, let’s write the function partialSum(), The full code for this function will be −const arr = [1, 1, 5, 2, -4, 6, 10]; const partialSum = (arr) => {    const output = [];    arr.forEach((num, index) => { ... Read More

How to remove some items from array when there is repetition in JavaScript

AmitDiwan
Updated on 09-Oct-2020 12:20:45

136 Views

We are required to write a JavaScript function that takes in an array of literals. Our function should return a new array with all the triplets filtered.The code for this will be −const arr1 = [1, 1, 1, 3, 3, 5]; const arr2 = [1, 1, 1, 1, 3, 3, 5]; const arr3 = [1, 1, 1, 3, 3, 3]; const arr4 = [1, 1, 1, 1, 3, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7, 7]; const removeTriplets = arr => {    const hashMap = arr => arr.reduce((acc, val) => { ... Read More

Fetching object keys using recursion in JavaScript

AmitDiwan
Updated on 09-Oct-2020 12:14:14

2K+ Views

We have an object with other objects being its property value, it is nested to 2-3 levels or even more.Here is the sample object −const people = {    Ram: {       fullName: 'Ram Kumar',       details: {          age: 31,          isEmployed: true       }    },    Sourav: {       fullName: 'Sourav Singh',       details: {          age: 22,          isEmployed: false       }    },    Jay: {       fullName: 'Jay ... Read More

Zig-Zag pattern in strings in JavaScript?

AmitDiwan
Updated on 09-Oct-2020 12:05:07

691 Views

We need to write a function that reads a string and converts the odd indexed characters in the string to upperCase and the even ones to lowerCase and returns a new string.Full code for doing the same will be −const text = 'Hello world, it is so nice to be alive.'; const changeCase = (str) => {    const newStr = str    .split("")    .map((word, index) => {       if(index % 2 === 0){          return word.toLowerCase();       }else{          return word.toUpperCase();       }    })   ... Read More

Building frequency map of all the elements in an array JavaScript

AmitDiwan
Updated on 09-Oct-2020 12:02:40

2K+ Views

We will be given an array of numbers / strings that contains some duplicate entries, all we have to do is to return the frequency of each element in the array.Returning an object with an element as key and its value as frequency would be perfect for this situation.We will iterate over the array with a forEach() loop and keep increasing the count of elements in the object if it already exists otherwise we will create a new property for that element in the object.And lastly, we will return the object.The full code for this problem will be −const arr ... Read More

Add line break inside a string conditionally in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:59:12

1K+ Views

We are required to write a function breakString() that takes in two arguments first the string to be broken and second is a number that represents the threshold count of characters after reaching which we have to repeatedly add line breaks in place of spaces.So, let’s do it. We will iterate over the with a for loop, we will keep a count that how many characters have elapsed with inserting a ‘’ if the count exceeds the limit and we encounter a space we replace it with line break in the new string and reset the count to 0 otherwise ... Read More

Natural Sort in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:57:34

898 Views

We have an array that contains some numbers and some strings, we are required to sort the array such that the numbers get sorted and get placed before every string and then the string should be placed sorted alphabetically.For exampleLet’s say this is our array −const arr = [1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9];The output should look like this −[1, 6, 7, 9, 47, 'afv', 'bdf', 'fdf', 'svd']Therefore, let’s write the code for this −const arr = [1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9]; const sorter = (a, b) => {    if(typeof a === ... Read More

JavaScript Strings: Replacing i with 1 and o with 0

AmitDiwan
Updated on 09-Oct-2020 11:55:05

270 Views

We are required to write a function that takes in a string as one and only argument and returns another string that has all ‘i’ and ‘o’ replaced with ‘1’ and ‘0’ respectively.It’s one of those classic for loop problems where we iterate over the string with its index and construct a new string as we move through.The code for the function will be −const string = 'Hello, is it raining in Amsterdam?'; const validate = (str) => {    let validatedString = '';    for(let i = 0; i < str.length; i++){       if(str[i] === 'a'){   ... Read More

Checking for the Gapful numbers in JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:53:24

127 Views

A number is a gapful number when −It has at least three digits, andIt is exactly divisible by the number formed by putting its first and last digits togetherFor example:1053 is a gapful number because it has 4 digits and it is exactly divisible by 13. 135 is a gapful number because it has 3 digits and it is exactly divisible by 15.Our job is to write a program that returns the nearest gapful number to the number we provide as input.Let’s write the code −const n = 134; //receives a number string and returns a boolean const isGapful = ... Read More

Advertisements