Found 9297 Articles for Object Oriented Programming

Converting Odd and Even-indexed characters in a string to uppercase/lowercase in JavaScript?

AmitDiwan
Updated on 19-Aug-2020 06:53:57

2K+ 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 −Exampleconst 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

Looping through and getting frequency of all the elements in an array JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:52:47

802 Views

Let’s say, 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 element as key and it’s 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 ... Read More

Add line break inside string only between specific position and only if there is a white space JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:51:34

428 Views

We are required to write a function, say breakString() that takes in two arguments: First, the string to be broken and second, a number that represents the threshold count of characters after reaching which we have to repeatedly add line breaks in place of spaces.For example −The following code should push a line break at the nearest space if 4 characters have passed without a line break −const text = 'Hey can I call you by your name?'; console.log(breakString(text, 4));Expected Output −Hey can I call you by your name?So, we will iterate over the with a for loop, we will ... Read More

Sort Array of numeric & alphabetical elements (Natural Sort) JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:50:19

866 Views

We have an array that contains some numbers and some strings. We are required to sort the array such that the numbers gets sorted and get placed before every string and then the string should be placed sorted alphabetically.For example −This array after being sortedconst arr = [1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9];Should look like this −[1, 6, 7, 9, 47, 'afv', 'bdf', 'fdf', 'svd']So, let’s write the code for this −Exampleconst arr = [1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9]; const sorter = (a, b) => {    if(typeof a === 'number' && typeof ... Read More

Validate input: replace all ‘a’ with ‘@’ and ‘i’ with ‘!’JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:48:43

75 Views

We are required to write a function validate() that takes in a string as one and only argument and returns another string that has all ‘a’ and ‘i’ replaced with ‘@’ and ‘!’ 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 −Exampleconst 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

Finding nearest Gapful number in JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:46:32

160 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 −The number 1053 is a gapful number because it has 4 digits and it is exactly divisible by 13. Similarly, 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.For example, for all 2-digit numbers, it would be 100. For 103, it would be ... Read More

How to remove “,” from a string in JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:45:08

128 Views

We are given a main string and a substring, our job is to create a function shedString() that takes in these two arguments and returns a version of the main string which is free of the substring.For example −shedString('12/23/2020', '/');should return a string −'12232020'Let’s now write the code for this function −Exampleconst shedString = (string, separator) => {    //we split the string and make it free of separator    const separatedArray = string.split(separator);    //we join the separatedArray with empty string    const separatedString = separatedArray.join("");    return separatedString; } const str = shedString('12/23/2020', '/'); console.log(str);OutputThe output of this ... Read More

Create a polyfill to replace nth occurrence of a string JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:43:56

404 Views

Let’s say, we have created a polyfill function removeStr() that takes in three arguments, namely −subStr → the occurrence of which String is to be removednum → it’s a Number (num)th occurrence of subStr is to be removed from stringThe function should return the new if the removal of the subStr from the string was successfully, otherwise it should return -1 in all cases.For example −const str = 'drsfgdrrtdr'; console.log(str.removeStr('dr', 3));Expected output −'drsfgdrrt'Let’s write the code for this −Exampleconst str = 'drsfgdrrtdr'; const subStr = 'dr'; const num = 2; removeStr = function(subStr, num){    if(!this.includes(subStr)){       return ... Read More

Finding out the Harshad number JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:42:30

430 Views

Harshad numbers are those numbers which are exactly divisible by the sum of their digits. Like the number 126, it is completely divisible by 1+2+6 = 9.All single digit numbers are harshad numbers.Harshad numbers often exist in consecutive clusters like [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [110, 111, 112], [1010, 1011, 1012].Our job is to write a function that takes in a Number as input checks whether it’s a harshad number or not, if not then returns -1 otherwise it returns the length of streak of consecutive harshad cluster.For example −harshadNum(1014) = harshadNum(1015) = harshadNum(1016) = ... Read More

How to create a random number between a range JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:39:28

178 Views

Our job is to create a function, say createRandom, that takes in two argument and returns a pseudorandom number between the range (max exclusive).The code for the function will be −Exampleconst min = 3; const max = 9; const createRandom = (min, max) => {    const diff = max - min;    const random = Math.random();    return Math.floor((random * diff) + min); } console.log(createRandom(min, max));Understanding the code −We take the difference of max and minWe create a random numberThen we multiply the diff and random to produce random number between 0 and diffThen we add min to it ... Read More

Advertisements