Found 9150 Articles for Object Oriented Programming

Sum of even numbers up to using recursive function in JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:32:23

701 Views

We have to write a recursive function that takes in a number n and returns the sum of all even numbers up to n.Let’s write the code for this function −Exampleconst recursiveEvenSum = (num, sum = 0) => {    num = num % 2 === 0 ? num : num - 1;    if(num){       return recursiveEvenSum(num - 2, sum+num);    }    return sum; }; console.log(recursiveEvenSum(12)); console.log(recursiveEvenSum(122)); console.log(recursiveEvenSum(23)); console.log(recursiveEvenSum(10)); console.log(recursiveEvenSum(19));OutputThe output in the console will be −42 3782 132 30 90

Verification if a number is Palindrome in JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:30:01

630 Views

Let’s say, we have to write a function that takes in a Number and returns a boolean based on the fact whether or not the number is palindrome. One restriction is that we have to do this without converting the number into a string or any other data type.Palindrome numbers are those numbers which read the same from both backward and forward.For example −121 343 12321Therefore, let’s write the code for this function −Exampleconst isPalindrome = (num) => {    // Finding the appropriate factor to extract the first digit    let factor = 1;    while (num / factor ... Read More

Reverse array with for loops JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:27:05

352 Views

We have to write a function that takes in an array and returns its reverse. Find its reverse using the for loop.Our sample array −const arr = [7, 2, 3, 4, 5, 7, 8, 12, -12, 43, 6];So, let’s write the code for this function −Exampleconst arr = [7, 2, 3, 4, 5, 7, 8, 12, -12, 43, 6]; const reverse =(arr) => {    const duplicate = arr.slice();    const reversedArray = [];    const { length } = arr;    for(let i = 0; i < length; i++){       reversedArray.push(duplicate.pop());    };    return reversedArray; }; console.log(reverse(arr));OutputThe output in the console will be −[    6, 43, -12, 12, 8,    7, 5, 4, 3, 2,    7 ]

Make array numbers negative JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:23:33

904 Views

Let’s say, the following is our array −const arr = [7, 2, 3, 4, 5, 7, 8, 12, -12, 43, 6];We are required to write a function that takes in the above array and returns an array with all the corresponding elements of array change to their negative counterpart (like 4 to -4, 6 to -6).If the element is already negative, then we should leave the element unchanged. Let’s write the code for this function −Exampleconst arr = [7, 2, 3, 4, 5, 7, 8, 12, -12, 43, 6]; const changeToNegative = (arr) => {    return arr.reduce((acc, val) => ... Read More

Adding only odd or even numbers JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:21:27

909 Views

We are required to make a function that given an array of numbers and a string that can take any of the two values “odd” or “even”, adds the numbers which match that condition. If no values match the condition, 0 should be returned.For example −console.log(conditionalSum([1, 2, 3, 4, 5], "even")); => 6 console.log(conditionalSum([1, 2, 3, 4, 5], "odd")); => 9 console.log(conditionalSum([13, 88, 12, 44, 99], "even")); => 144 console.log(conditionalSum([], "odd")); => 0So, let’s write the code for this function, we will use the Array.prototype.reduce() method here −Exampleconst conditionalSum = (arr, condition) => {    const add = (num1, num2) ... Read More

Append the current array with the squares of corresponding elements of the array in JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:19:32

109 Views

We have an array of Numbers like this −const arr = [12, 19, 5, 7, 9, 11, 21, 4];We have to write a function that takes in such an array and returns a new array with all the items of the original array appended by the squares of corresponding elements of the array.For this sample array, the output should be −[12, 19, 5, 7, 9, 11, 21, 4, 144, 361, 25, 49, 81, 121, 441, 16]Exampleconst arr = [12, 19, 5, 7, 9, 11, 21, 4]; const multiplyArray = (arr) => {    return arr.reduce((acc, val) => {       return acc.concat(val * val);    }, arr); }; console.log(multiplyArray(arr));OutputThe output in the console will be −[    12, 19, 5, 7, 9, 11,    21, 4, 144, 361, 25, 49,    81, 121, 441, 16 ]

Convert JavaScript array iteration result into a single line text string

AmitDiwan
Updated on 21-Aug-2020 14:16:44

296 Views

Let’s say, we have a string and an array −const textString = 'Convert javascript array iteration result into a single line text string. Happy searching!'; const keywords = ['integer', 'javascript', 'dry', 'Happy', 'exam'];We have to write a function that maps the array to a string containing only true and false depending on the fact whether the corresponding array element is present in the string or not.Exampleconst textString = 'Convert javascript array iteration result into a single line text string. Happy searching!'; const keywords = ['integer', 'javascript', 'dry', 'Happy', 'exam']; const includesString = (arr, str) => {    return arr.reduce((acc, val) ... Read More

Inverse operation in JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:13:52

910 Views

Let’s say, we have to write a function that takes in a binary string (consisting of only 0 and 1) and returns its inverse, all 0s replaced by 1 and 1s replaced by 0.Let’s write the code for this function −Exampleconst num = '1101'; const n = '11010111'; const inverseBinary = (binary) => {    return binary.split("").map(el => {       return `${1- parseInt(el, 10)}`    }).join(""); }; console.log(inverseBinary(num)); console.log(inverseBinary(n));OutputThe output in the console will be −0010 00101000

Merge objects in array with similar key JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:10:14

2K+ Views

Let’s say, we have the following array of objects −const arr = [    {id: 1, h1: 'Daily tests'},    {id: 2, h1: 'Details'},    {id: 1, h2: 'Daily classes'},    {id: 3, h2: 'Results'},    {id: 2, h3: 'Admissions'},    {id: 1, h4: 'Students'},    {id: 2, h5: 'Alumni'},    {id: 3, h3: 'Appreciations'},    {id: 1, h5: 'Tiny Tots'},    {id: 1, h6: 'Extras'}, ];We have to write a function that converts this array into an array where all the headings (h1, h2, h3 …) that have the same id get clubbed inside the same object. Therefore, let’s ... Read More

Order an array of words based on another array of words JavaScript

AmitDiwan
Updated on 21-Aug-2020 14:07:19

342 Views

Let’s say, we have the following array of objects sorted according to its id property −const unordered = [{    id: 1,    string: 'sometimes' }, {    id: 2,    string: 'be' }, {    id: 3,    string: 'can' }, {    id: 4,    string: 'life' }, {    id: 5,    string: 'tough' }, {    id: 6,    string: 'very' }, ];And another array of string like this −const ordered = ['Life', 'sometimes', 'can', 'be', 'very', 'tough'];We have to sort the first array so its string property have the same order of string as it ... Read More

Advertisements