Found 9317 Articles for Object Oriented Programming

Neutralisation of strings - JavaScript

Naveen Singh
Updated on 16-Sep-2020 09:51:07

159 Views

We are required to write a JavaScript function that takes in a string that contains only '+' or '-' and we have to return either '+' or '-' based on the whole neutralisation result of the string.Like '++' results to '+' and '--' also results to '+' while '-+' or '+-' results to '-'.Following is our string −const str = '+++-+-++---+-+--+-';ExampleFollowing is the code −const str = '+++-+-++---+-+--+-'; const netResult = (str = '') => {    const strArr = str.split('');    return strArr.reduce((acc, val) => {       if(acc === val){          return '+';   ... Read More

Checking Oddish and Evenish numbers - JavaScript

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

195 Views

A number is Oddish if the sum of all of its digits is odd, and a number is Evenish if the sum of all of its digits is even.We are required to write a function that determines whether a number is Oddish or Evenish. We should return true of Oddish values and false for evenishExampleFollowing is the code −const num = 434667; const isOddish = (num, sum = 0) => {    if(num){       return isOddish(Math.floor(num / 10), sum + (num % 10));    };    return sum % 2 === 1; }; console.log(isOddish(num));OutputFollowing is the output in the console −false

Product of two number using HOC - JavaScript

Naveen Singh
Updated on 16-Sep-2020 09:48:45

191 Views

HOCHOC or Higher Order Functions in JavaScript are a special type of functions that receives another function as argument or have a function set as their return value or do both. HOC along with closures is a very powerful tool in JavaScript.We are required to write a JavaScript Higher Order Function that can be used to obtain the product of two numbers.ExampleFollowing is the code −const num1 = 24; const num2 = 5; const productHOC = num1 => {    return product = num2 => {       return num1 * num2;    }; }; console.log(productHOC(num1)(num2));OutputFollowing is the output in the console −120

Removing 0s from start and end - JavaScript

Naveen Singh
Updated on 16-Sep-2020 09:38:09

86 Views

We are required to write a JavaScript function that takes in a number as a string and returns a new number string with all the leading and trailing 0s removedFor example: If the input is −const strNum = '054954000'Then the output should be −const output = '54954'ExampleFollowing is the code −const strNum = '054954000'; const removeZero = (str = '') => {    const res = '';    let startLen = 0, endLen = str.length-1;    while(str[startLen] === '0'){       startLen++;    };    while(str[endLen] === '0'){       endLen--;    };    return str.substring(startLen, endLen+1); }; console.log(removeZero(strNum));OutputFollowing is the output in the console −54954

Finding special array - JavaScript

Naveen Singh
Updated on 16-Sep-2020 09:36:10

392 Views

An array is a special array if −--All the elements at odd indices are odd. --All the elements at even indices are even.We are required to write a JavaScript function that takes in an array and checks if its a special array or not.ExampleFollowing is the code −const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const isSpecial = (arr = []) => {    for(let i = 0; i < arr.length; i++){       if(arr[i] % 2 === i % 2){          continue;       };       return false;    };    return true; }; console.log(isSpecial(arr));OutputFollowing is the output in the console −true

Move string capital letters to front maintaining the relative order - JavaScript

Naveen Singh
Updated on 16-Sep-2020 09:35:03

204 Views

We are required to write a JavaScript function that takes in a string with uppercase and lowercase letters. The function should return a string with all the uppercase letters moved to front of the string.For example: If the input string is −const str = 'heLLO woRlD';Then the output should be −const output = 'LLORDhe wol';ExampleFollowing is the code −const str = 'heLLO woRlD'; const moveCapitalToFront = (str = '') => {    let capitalIndex = 0;    const newStrArr = [];    for(let i = 0; i < str.length; i++){       if(str[i] !== str[i].toLowerCase()){         ... Read More

Concatenating variable number of arrays into one - JavaScript

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

428 Views

We are required to write a JavaScript function that takes in any number of JavaScript arrays and returns one single array with all the values from input arrays concatenated into it.For example − If the input arrays are −[1, 5], [44, 67, 3], [2, 5], [7], [4], [3, 7], [6]Then the output should be −const output = [1, 5, 44, 67, 3, 2, 5, 7, 4, 3, 7, 6];ExampleFollowing is the code −const a = [1, 5], b = [44, 67, 3], c = [2, 5], d = [7], e = [4], f = [3, 7], g = [6]; const concatArrays = (...arr) => {    const res = arr.reduce((acc, val) => {       return acc.concat(...val);    }, []);    return res; }; console.log(concatArrays(a, b, c, d, e, f, g));OutputFollowing is the output in the console −[    1, 5, 44, 67, 3,    2, 5,  7,  4, 3,    7, 6 ]

Array of multiples - JavaScript

Naveen Singh
Updated on 16-Sep-2020 09:32:46

769 Views

We are required to write a JavaScript function that takes in two numbers, say m and n, and it returns an array of first n multiples of m.For example − If the numbers are 4 and 6Then the output should be −const output = [4, 8, 12, 16, 20, 24]ExampleFollowing is the code −const num1 = 4; const num2 = 6; const multiples = (num1, num2) => {    const res = [];    for(let i = num1; i

Finding tidy numbers - JavaScript

Naveen Singh
Updated on 16-Sep-2020 09:31:44

170 Views

A tidy number is a number whose digits are in non-decreasing order. We are required to write a JavaScript function that takes in a number and checks whether its a tidy number or not.For example −489 is a tidy number 234557 is also a tidy number 34535 is not a tidy numberExampleFollowing is the code −const num = 234789; const isTidy = (num, last = 10) => {    if(num){       if(num % 10 > last){          return false;       };       return isTidy(Math.floor(num / 10), (num % 10));    };    return true; }; console.log(isTidy(num));OutputFollowing is the output in the console −true

Number of smaller and larger elements - JavaScript

Naveen Singh
Updated on 16-Sep-2020 09:30:24

357 Views

Suppose, we have an array of literals like this −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9];We are required to write a JavaScript function that takes in this array and a number, say n, and returns an object representing the count of elements greater than and smaller than n.ExampleFollowing is the code −const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9]; const greaterSmallerNumbers = (arr, num) => {    return arr.reduce((acc, val) => {       let { greater, smaller } = acc;       if(val > num){          greater++;       };       if(val < num){          smaller++;    };    return { greater, smaller };    }, {       greater: 0,       smaller: 0    }); }; console.log(greaterSmallerNumbers(arr, 3));OutputFollowing is the output in the console −{ greater: 8, smaller: 1 }

Advertisements