Found 9317 Articles for Object Oriented Programming

Finding second smallest word in a string - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:33:56

227 Views

We are required to write a JavaScript function that takes in a string sentence as first and the only argument. The function should return the length of the second smallest word from the string.For example: If the string is −const str = 'This is a sample string';Then the output should be 2.ExampleFollowing is the code −const str = 'This is a sample string'; const secondSmallest = str => {    const strArr = str.split(' ');    if(strArr.length < 2){       return false;    }    for(let i = 0; i < strArr.length; i++){       strArr[i] = ... Read More

Expressing numbers in expanded form - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:32:51

594 Views

Suppose we are given a number 124 and are required to write a function that takes this number as input and returns its expanded form as a string.The expanded form of 124 is −'100+20+4'ExampleFollowing is the code −const num = 125; const expandedForm = num => {    const numStr = String(num);    let res = '';    for(let i = 0; i < numStr.length; i++){       const placeValue = +(numStr[i]) * Math.pow(10, (numStr.length - 1 - i));       if(numStr.length - i > 1){          res += `${placeValue}+`       }else{          res += placeValue;       };    };    return res; }; console.log(expandedForm(num));OutputFollowing is the output in the console −100+20+5

Replacing every nth instance of characters in a string - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:31:16

786 Views

We are required to write a JavaScript function that takes in a string as the first argument, a number, say n, as the second argument and a character, say c, as the third argument. The function should replace the nth appearance of any character with the character provided as the third argument and return the new string.ExampleFollowing is the code −const str = 'This is a sample string'; const num = 2; const char = '*'; const replaceNthAppearance = (str, num, char) => {    const creds = str.split('').reduce((acc, val, ind, arr) => {       let { res, ... Read More

Finding the element larger than all elements on right - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:28:58

113 Views

We are required to write a JavaScript function that takes in an array of numbers and returns a subarray that contains all the element from the original array that are larger than all the elements on their right.ExampleFollowing is the code −const arr = [12, 45, 6, 4, 23, 23, 21, 1]; const largerThanRight = (arr = []) => {    const creds = arr.reduceRight((acc, val) => {       let { largest, res } = acc;       if(val > largest){          res.push(val);          largest = val;       };       return { largest, res };    }, {       largest: -Infinity,       res: []    });    return creds.res; }; console.log(largerThanRight(arr));OutputFollowing is the output in the console −[ 1, 21, 23, 45 ]

Check for Valid hex code - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:27:29

546 Views

A string can be considered as a valid hex code if it contains no characters other than the 0-9 and a-f alphabetsFor example −'3423ad' is a valid hex code '4234es' is an invalid hex codeWe are required to write a JavaScript function that takes in a string and checks whether its a valid hex code or not.ExampleFollowing is the code −const str1 = '4234es'; const str2 = '3423ad'; const isHexValid = str => {    const legend = '0123456789abcdef';    for(let i = 0; i < str.length; i++){       if(legend.includes(str[i])){          continue;       ... Read More

Finding persistence of number in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:25:50

273 Views

We are required to write a JavaScript function that takes in an positive integer and returns its additive persistenceThe additive persistence of an integer, say n, is the number of times we have to replace the number with the sum of its digits until the number becomes a single digit integer.For example −If the number is −1679583Then, 1 + 6 + 7 + 9 + 5 + 8 + 3 = 39   // 1 Pass 3 + 9 = 12                   // 2 Pass 1 + 2 = 3     ... Read More

Can array form consecutive sequence - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:24:28

115 Views

We are required to write a JavaScript function that takes in an array of numbers and checks if the elements of the array can be rearranged to form a sequence of numbers or not.For example −If the array is −const arr = [3, 1, 4, 2, 5];Then the output should be −trueExampleFollowing is the code −const arr = [3, 1, 4, 2, 5]; const canBeConsecutive = (arr = []) => {    if(!arr.length){       return false;    };    const copy = arr.slice();    copy.sort((a, b) => a - b);    for(let i = copy[0], j = 0; ... Read More

Checking smooth sentences in JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:23:17

181 Views

We are required to write a JavaScript function that checks whether a sentence is smooth or not. A sentence is smooth when the first letter of each word in the sentence is same as the last letter of its preceding word.ExampleFollowing is the code −const str = 'this stringt tries sto obe esmooth'; const str2 = 'this string is not smooth'; const isSmooth = str => {    const strArr = str.split(' ');    for(let i = 0; i < strArr.length; i++){       if(!strArr[i+1] || strArr[i][strArr[i].length -1] === strArr[i+1]       [0]){          continue; ... Read More

Strictly increasing or decreasing array - JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:21:57

297 Views

In Mathematics, a strictly increasing function is that function in which the value to be plotted always increase. Similarly, a strictly decreasing function is that function in which the value to be plotted always decrease.We are required to write a JavaScript function that takes in an array of numbers and returns true if it’s either strictly increasing or strictly decreasing, otherwise returns false.ExampleFollowing is the code −const arr = [12, 45, 6, 4, 23, 23, 21, 1]; const arr2 = [12, 45, 67, 89, 123, 144, 2656, 5657]; const sameSlope = (a, b, c) => (b - a < 0 && c - b < 0) || (b - a > 0 && c - b > 0); const increasingOrDecreasing = (arr = []) => {    if(arr.length

Determining rightness of a triangle – JavaScript

AmitDiwan
Updated on 16-Sep-2020 10:20:47

576 Views

We are required to write a JavaScript function that takes in three numbers say a, b and c representing the length of three sides of a triangle. The function should return true if those three sides represent a right-angle triangle, false otherwise.Right angle triangleA triangle is a right-angle triangle if one of the three angles in the triangle is 90 degree. And one angle in the triangle is 90 degrees when the square of the longest side is equal to the sum of squares of the other two sides.For example − 3, 4, 5, as3*3 + 4*4 = 5*5 = ... Read More

Advertisements