AmitDiwan has Published 10744 Articles

Fetching object keys using recursion in JavaScript

AmitDiwan

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,     ... Read More

Array grouping on the basis of children object’s property in JavaScript

AmitDiwan

AmitDiwan

Updated on 09-Oct-2020 12:08:44

243 Views

We have an array of objects that contains data about some cars. The array is given as follows −const cars = [{    company: 'Honda',    type: 'SUV' }, {    company: 'Hyundai',    type: 'Sedan' }, {    company: 'Suzuki',    type: 'Sedan' }, {    company: 'Audi',   ... Read More

Zig-Zag pattern in strings in JavaScript?

AmitDiwan

AmitDiwan

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

638 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 ... Read More

Building frequency map of all the elements in an array JavaScript

AmitDiwan

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 ... Read More

Add line break inside a string conditionally in JavaScript

AmitDiwan

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 ... Read More

Natural Sort in JavaScript

AmitDiwan

AmitDiwan

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

847 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', ... Read More

JavaScript Strings: Replacing i with 1 and o with 0

AmitDiwan

AmitDiwan

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

226 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 ... Read More

Checking for the Gapful numbers in JavaScript

AmitDiwan

AmitDiwan

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

98 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 ... Read More

Removing a specific substring from a string in JavaScript

AmitDiwan

AmitDiwan

Updated on 09-Oct-2020 11:51:00

424 Views

We are given a main string and a substring, our job is to create a function, let’s say removeString() that takes in these two arguments and returns a version of the main string which is free of the substring.Here, we need to remove the separator from a string, for example ... Read More

String function to replace nth occurrence of a character in a string JavaScript

AmitDiwan

AmitDiwan

Updated on 09-Oct-2020 11:47:30

1K+ Views

We are required to write a function removeStr() that lives on String.prototype object and takes in a string str, a character char and a number n.The function should remove the nth appearance of char from str.Let’s write the code for this −const str = 'aaaaaa'; const subStr = 'a'; const ... Read More

Advertisements