Found 9316 Articles for Object Oriented Programming

Converting multi-dimensional array to string in JavaScript

AmitDiwan
Updated on 15-Oct-2020 08:57:16

1K+ Views

We are required to write a JavaScript function that takes in a nested array of literals and converts it to a string by concatenating all the values present in it to the string. Moreover, we should append a whitespace at the end of each string element while constructing the new string.Let’s write the code for this function −ExampleThe code for this will be −const arr = [    'this', [       'is', 'an', [          'example', 'of', [             'nested', 'array'          ]       ] ... Read More

ASCII sum difference of strings in JavaScript

AmitDiwan
Updated on 15-Oct-2020 08:53:55

479 Views

ASCII Code:ASCII is a 7-bit character code where every single bit represents a unique character. Every English alphabet has a unique decimal ascii code.We are required to write a function that takes in two strings and calculates their ascii scores (i.e., the sum of ascii decimal of each character of string) and returns the difference.Let’s write the code for this function −ExampleThe code for this will be −const str1 = 'This is an example sting'; const str2 = 'This is the second string'; const calculateScore = (str = '') => {    return str.split("").reduce((acc, val) => {       ... Read More

Prime numbers within a range in JavaScript

AmitDiwan
Updated on 15-Oct-2020 08:51:24

601 Views

We are required to write a JavaScript function that takes in two numbers, say, a and b and returns the total number of prime numbers between a and b (including a and b, if they are prime).For example: If a = 21, and b = 38.The prime numbers between them are 23, 29, 31, 37And their count is 4Our function should return 4ExampleThe code for this will be −const isPrime = num => {    let count = 2;    while(count < (num / 2)+1){       if(num % count !== 0){          count++;          continue;       };       return false;    };    return true; }; const primeBetween = (a, b) => {    let count = 0;    for(let i = Math.min(a, b); i

Removing first k characters from string in JavaScript

AmitDiwan
Updated on 15-Oct-2020 08:49:16

124 Views

We are required to write a JavaScript function that takes in a string and a number, say k and returns another string with first k characters removed from the string.For example: If the original string is −const str = "this is a string"and, n = 4then the output should be −const output = " is a string"ExampleThe code for this will be −const str = 'this is a string'; const removeN = (str, num) => {    const { length } = str;    if(num > length){       return str;    };    const newStr = str.substr(num, length ... Read More

Smart concatenation of strings in JavaScript

AmitDiwan
Updated on 15-Oct-2020 08:43:22

120 Views

We are required to write a JavaScript function that takes in two strings and concatenates the second string to the first string.If the last character of the first string and the first character of the second string are the same then we have to omit one of those characters.ExampleThe code for this will be −const str1 = 'Food'; const str2 = 'dog'; const concatenateStrings = (str1, str2) => {    const { length: l1 } = str1;    const { length: l2 } = str2;    if(str1[l1 - 1] !== str2[0]){       return str1 + str2;    }; ... Read More

Check for perfect square in JavaScript

AmitDiwan
Updated on 15-Oct-2020 08:41:47

845 Views

We are required to write a JavaScript function that takes in a number and returns a boolean based on the fact whether or not the number is a perfect square.Examples of perfect square numbers −Some perfect square numbers are −144, 196, 121, 81, 484ExampleThe code for this will be −const num = 484; const isPerfectSquare = num => {    let ind = 1;    while(ind * ind

Finding sum of every nth element of array in JavaScript

AmitDiwan
Updated on 15-Oct-2020 08:39:59

825 Views

We are required to write a JavaScript function that takes in an array of numbers and returns the cumulative sum of every number present at the index that is a multiple of n from the array.ExampleThe code for this will be −const arr = [5, 3, 5, 6, 12, 5, 65, 3, 2]; const num = 3; const nthSum = (arr, num) => {    let sum = 0;    for(let i = 0; i < arr.length; i++){       if(i % num !== 0){          continue;       };       sum += arr[i];    };    return sum; }; console.log(nthSum(arr, num));OutputThe output in the console −76

Excluding extreme elements from average calculation in JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:21:22

139 Views

We are required to write a JavaScript function that takes in an array of Number. Then the function should return the average of its elements excluding the smallest and largest Number.ExampleThe code for this will be −const arr = [5, 3, 5, 6, 12, 5, 65, 3, 2]; const findExcludedAverage = arr => {    const creds = arr.reduce((acc, val) => {       let { min, max, sum } = acc;       sum += val;       if(val > max){          max = val;       };       if(val < min){          min = val;       };       return { min, max, sum };    }, {       min: Infinity,       max: -Infinity,       sum: 0    });    const { max, min, sum } = creds;    return (sum - min - max) / (arr.length / 2); }; console.log(findExcludedAverage(arr));OutputThe output in the console −8.666666666666666

Equality of corresponding elements in JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:19:50

134 Views

We are required to write a JavaScript function that takes in two arrays of literals. The function should check the corresponding elements of the array. The function should return true if all the corresponding elements of the array are equal otherwise it should return false.ExampleThe code for this will be −const arr1 = [6, 7, 8, 9, 10, 11, 12, 14]; const arr2 = [6, 7, 8, 9, 10, 11, 12, 14]; const areEqual = (first, second) => {    if(first.length !== second.length){       return false;    };    for(let i = 0; i < first.length; i++){       if(first[i] === second[i]){          continue;       }       return false;    };    return true; }; console.log(areEqual(arr1, arr2));OutputThe output in the console −True

Splitting a string into parts in JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:18:15

248 Views

We are required to write a JavaScript function that takes in a string and a number n (such that n exactly divides the length of string) and we need to return an array of string of length n containing n equal parts of the string.ExampleThe code for this will be −const str = 'we will be splitting this string into parts'; const num = 6; const divideEqual = (str, num) => {    const len = str.length / num;    const creds = str.split("").reduce((acc, val) => {       let { res, currInd } = acc;       ... Read More

Advertisements