Found 9317 Articles for Object Oriented Programming

Converting string to a binary string - JavaScript

Naveen Singh
Updated on 16-Sep-2020 10:04:13

512 Views

We are required to write a JavaScript function that takes in a lowercase string and returns a new string in which all the elements between [a, m] are represented by 0 and all the elements between [n, z] are represented by 1.ExampleFollowing is the code −const str = 'Hello worlld how are you'; const stringToBinary = (str = '') => {    const s = str.toLowerCase();    let res = '';    for(let i = 0; i < s.length; i++){       // for special characters       if(s[i].toLowerCase() === s[i].toUpperCase()){          res += s[i]; ... Read More

Finding greatest digit by recursion - JavaScript

Naveen Singh
Updated on 16-Sep-2020 10:02:46

241 Views

We are required to write a JavaScript recursive function that takes in a number and returns the greatest digit in the number.For example: If the number is −45654356Then the return value should be 6ExampleFollowing is the code −const num = 45654356; const greatestDigit = (num = 0, greatest = 0) => {    if(num){       const max = Math.max(num % 10, greatest);       return greatestDigit(Math.floor(num / 10), max);    };    return greatest; }; console.log(greatestDigit(num));OutputFollowing is the output in the console −6

Calculating resistance of n devices - JavaScript

Naveen Singh
Updated on 16-Sep-2020 10:01:45

140 Views

In Physics, the equivalent resistance of say 3 resistors connected in series is given by −R = R1 + R2 + R3And the equivalent resistance of resistors connected in parallel is given by −R = (1/R1) + (1/R2) + (1/R3)We are required to write a JavaScript function that takes a string having two possible values, 'series' or 'parallel' and then n numbers representing the resistance of n resistors.And the function should return the equivalent resistance of these resistors.ExampleLet us write the code for this function.const r1 = 5, r2 = 7, r3 = 9; const equivalentResistance = (combination = 'parallel', ... Read More

Distance to nearest vowel in a string - JavaScript

Naveen Singh
Updated on 16-Sep-2020 10:00:07

392 Views

We are required to write a JavaScript function that takes in a string with atleast one vowel, and for each character in the string we have to map a number in a string representing its nearest distance from a vowel.For example: If the string is −const str = 'vatghvf';Then the output should be −const output = [1, 0, 1, 2, 3, 4, 5];ExampleFollowing is the code −const str = 'vatghvf'; const nearest = (arr = [], el) => arr.reduce((acc, val) => Math.min(acc, Math.abs(val - el)), Infinity); const vowelNearestDistance = (str = '') => {    const s = str.toLowerCase();   ... Read More

Valid triangle edges - JavaScript

Naveen Singh
Updated on 16-Sep-2020 09:58:25

382 Views

Suppose, we have three lines of length, respectively l, m and n. These three lines can only form a triangle, if the sum of any arbitrary two sides is greater than the third side.For example, if the length of three lines is 4, 9 and 3, they cannot form a triangle because 4+3 is less than 9.We are required to write a JavaScript function that the three numbers represents the length of three sides and checks whether they can form a triangle or not.ExampleFollowing is the code −const a = 9, b = 5, c = 3; const isValidTriangle = ... Read More

Casting a string to snake case - JavaScript

Naveen Singh
Updated on 16-Sep-2020 09:57:22

2K+ Views

Snake case is basically a style of writing strings by replacing the spaces with '_' and converting the first letter of each word to lowercase.We are required to write a JavaScript function that takes in a string and converts it to snake case.ExampleFollowing is the code −const str = 'This is a simple sentence'; const toSnakeCase = (str = '') => {    const strArr = str.split(' ');    const snakeArr = strArr.reduce((acc, val) => {       return acc.concat(val.toLowerCase());    }, []);    return snakeArr.join('_'); }; console.log(toSnakeCase(str));OutputFollowing is the output in the console −this_is_a_simple_sentence

Return index of first repeating character in a string - JavaScript

Naveen Singh
Updated on 16-Sep-2020 09:56:20

156 Views

We are required to write a JavaScript function that takes in a string and returns the index of first character that appears twice in the string.If there is no such character then we should return -1. Following is our string −const str = 'Hello world, how are you';ExampleFollowing is the code −const str = 'Hello world, how are you'; const firstRepeating = str => {    const map = new Map();    for(let i = 0; i < str.length; i++){       if(map.has(str[i])){          return map.get(str[i]);       };       map.set(str[i], i);   ... Read More

Find the dissimilarities in two strings - JavaScript

Naveen Singh
Updated on 16-Sep-2020 09:54:57

96 Views

We are required to write a JavaScript function that takes in two strings and find the number of corresponding dissimilarities in the stringsThe corresponding elements will be dissimilar if they are not equal. Let’s say the following are our two string −const str1 = 'Hello world!!!'; const str2 = 'Hellp world111';ExampleFollowing is the code −const str1 = 'Hello world!!!'; const str2 = 'Hellp world111'; const dissimilarity = (str1 = '', str2 = '') => {    let count = 0;    for(let i = 0; i < str1.length; i++){       if(str1[i] === str2[i]){          continue; ... Read More

Reverse only the odd length words - JavaScript

Naveen Singh
Updated on 16-Sep-2020 09:53:47

466 Views

We are required to write a JavaScript function that takes in a string and reverses the words in the string that have an odd number of characters in them.Any substring in the string qualifies to be a word, if either it is encapsulated by two spaces on either ends or present at the end or beginning and followed or preceded by a space.Let’s say the following is our string −const str = 'hello beautiful people';The odd length words are −hello beautifulExampleLet us write the code for this function.const str = 'hello beautiful people'; const idOdd = str => str.length % ... Read More

Reverse word starting with particular characters - JavaScript

Naveen Singh
Updated on 16-Sep-2020 09:52:22

175 Views

We are required to write a JavaScript function that takes in a sentence string and a character and the function should reverse all the words in the string starting with that particular character.For example: If the string is −const str = 'hello world, how are you';Starting with a particular character ‘h’ −Then the output string should be −const output = 'olleh world, woh are you';That means, we have reversed the words starting with “h” i.e. Hello and How.ExampleFollowing is the code −const str = 'hello world, how are you'; const reverseStartingWith = (str, char) => {    const strArr = ... Read More

Advertisements