Found 9316 Articles for Object Oriented Programming

Deleting the last vowel from a string in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:33:36

192 Views

We are required to write a JavaScript function that takes in a string and returns a new string with the last vowel of each word removed.For example: If the string is −const str = 'This is an example string';Then the output should be −const output = 'Ths s n exampl strng';Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'This is an example string'; const removeLast = word => {    const lastIndex = el => word.lastIndexOf(el);    const ind = Math.max(lastIndex('a'), lastIndex('e'), lastIndex('i'), lastIndex('o'), lastIndex('u'));    return word.substr(0, ind) + word.substr(ind+1, ... Read More

Performing the subtraction operation without the subtraction operator in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:31:45

106 Views

We are required to write a JavaScript function that takes in two numbers and returns their difference but without using the (-) sign.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num1 = 56; const num = 78; const subtractWithoutMinus = (num1, num2) => {    if(num2 === 0){       return num1;    };    return subtractWithoutMinus(num1 ^ num2, (~num1 & num2)

Picking the odd one out in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:30:24

408 Views

We are required to write a JavaScript function that takes in an array of literals that contains all similar elements but one.Our function should return the unlike number.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]; // considering that the length of array is atleast 3 const findUnlike = arr => {    for(let i = 1; i < arr.length-1; i++){       if(arr[i] - arr[i-1] !== 0 && arr[i]-arr[i+1] === 0){          return arr[i-1];     ... Read More

Interchanging a string to a binary string in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:25:17

108 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.ExampleThe code for this will be −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 ... Read More

Finding nearest prime to a specified number in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:23:36

172 Views

We are required to write a JavaScript function that takes in a number and returns the first prime number that appears after n.For example: If the number is 24, then the output should be 29.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 24; const isPrime = n => {    if (n===1){       return false;    }else if(n === 2){       return true;    }else{       for(let x = 2; x < n; x++){          if(n % x === 0){             return false;          }       }       return true;    }; }; const nearestPrime = num => {    while(!isPrime(++num)){};    return num; }; console.log(nearestPrime(24));OutputThe output in the console will be −29

Generating random string of specified length in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:20:28

357 Views

We are required to write a JavaScript function that takes in a number n and returns a random string of length n containing no other than the 26 English lowercase alphabets.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 8; const randomNameGenerator = num => {    let res = '';    for(let i = 0; i < num; i++){       const random = Math.floor(Math.random() * 27);       res += String.fromCharCode(97 + random);    };    return res; }; console.log(randomNameGenerator(num));OutputThe output in the console will be −kdcwpingNote − This is one of many possible outputs. Console output is expected to differ every time.

Pairing an array from extreme ends in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:19:14

130 Views

We are required to write a JavaScript function that takes in an array of Number / String literals and returns another array of arrays. With each subarray containing exactly two elements, the nth element from start nth from last.For example: If the array is −const arr = [1, 2, 3, 4, 5, 6];Then the output should be −const output = [[1, 6], [2, 5], [3, 4]];ExampleThe code for this will be −const arr = [1, 2, 3, 4, 5, 6]; const edgePairs = arr => {    const res = [];    const upto = arr.length % 2 === 0 ... Read More

Return a map representing the frequency of each data type in an array in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:17:31

73 Views

We are required to write a JavaScript function that takes in an array that contains elements of different data types and the function should return a map representing the frequency of each data type.Let’s say the following is our array −const arr = [23, 'df', undefined, null, 12, {    name: 'Rajesh' }, [2, 4, 7], 'dfd', null, Symbol('*'), 8];ExampleFollowing is the code to return a map representing the frequency of each datatype −const arr = [23, 'df', undefined, null, 12, {    name: 'Rajesh' }, [2, 4, 7], 'dfd', null, Symbol('*'), 8]; const countDataTypes = arr => {   ... Read More

Finding the continuity of two arrays in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:15:50

101 Views

We are required to write a JavaScript function that takes in two arrays of numbers. And the function should return true if the two arrays upon combining and shuffling can form a consecutive sequence, false otherwise.For example: If the arrays are −const arr1 = [4, 6, 2, 9, 3]; const arr2 = [1, 5, 8, 7];Then the output should be true.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = [4, 6, 2, 9, 3]; const arr2 = [1, 5, 8, 7]; const canFormSequence = (arr1, arr2) => {    const combined = ... Read More

Function to find the length of the second smallest word in a string in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:13:53

108 Views

We are required to write a JavaScript function that takes in a string sentence as first and the only argument. And 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.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'This is a sample string'; const secondSmallest = str => {    const strArr = str.split(' ');    if(strArr.length < 2){       return false;    }    for(let i ... Read More

Advertisements