Found 9317 Articles for Object Oriented Programming

Find distinct elements - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:27:48

101 Views

We are required to write a JavaScript function that takes in an array of literals, such that some array elements are repeated. We are required to return an array that contains that appear only once (not repeated).For example: If the array is:>const arr = [9, 5, 6, 8, 7, 7, 1, 1, 1, 1, 1, 9, 8];Then the output should be −const output = [5, 6];ExampleFollowing is the code −const arr = [9, 5, 6, 8, 7, 7, 1, 1, 1, 1, 1, 9, 8]; const findDistinct = arr => {    const res = [];    for(let i = 0; i < arr.length; i++){       if(arr.indexOf(arr[i]) !== arr.lastIndexOf(arr[i])){          continue;       };       res.push(arr[i]);    };    return res; }; console.log(findDistinct(arr));OutputFollowing is the output in the console −[5, 6]

Filtering out primes from an array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:26:21

1K+ Views

We are required to write a JavaScript function that takes in the following array of numbers,const arr = [34, 56, 3, 56, 4, 343, 68, 56, 34, 87, 8, 45, 34];and returns a new filtered array that does not contain any prime number.ExampleFollowing is the code −const arr = [34, 56, 3, 56, 4, 343, 68, 56, 34, 87, 8, 45, 34]; 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 filterPrime = arr => {    const filtered = arr.filter(el => !isPrime(el));    return filtered; }; console.log(filterPrime(arr));OutputFollowing is the output in the console −[    34, 56, 56,  4, 343,    68, 56, 34, 87,   8,    45, 34 ]

Resolving or rejecting promises accordingly - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:25:16

55 Views

We are required to write a JavaScript function that imitates a network request, for that we can use the JavaScript setTimeout() function, that executes a task after a given time interval.Our function should return a promise that resolves when the request takes place successfully, otherwise it rejectsExampleFollowing is the code −const num1 = 45, num2 = 48; const res = 93; const expectedSumToBe = (num1, num2, res) => {    return new Promise((resolve, reject) => {       setTimeout(() => {          if(num1 + num2 === res){             resolve('success');     ... Read More

Frequency distribution of elements - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:22:50

896 Views

We are required to write a JavaScript function that contains the following string −const str = 'This string will be used to calculate frequency distribution';We need to return an object that represents the frequency distribution of various elements present in the array.ExampleFollowing is the code −const str = 'This string will be used to calculate frequency distribution'; const frequencyDistribution = str => {    const map = {};    for(let i = 0; i < str.length; i++){       map[str[i]] = (map[str[i]] || 0) + 1;    };    return map; }; console.log(frequencyDistribution(str));OutputFollowing is the output in the console ... Read More

Finding mistakes in a string - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:21:38

378 Views

We are required to write a JavaScript function that takes in two strings. The first string is some mistyped string and the second string is the correct version of this sting. We can assume that the two strings we are getting as argument will always have the same length.We have to return the number of mistakes that exist in the first array.ExampleFollowing is the code −const str1 = 'Tkos am er exakgrg fetwnh'; const str2 = 'This is an example string'; const countMistakes = (mistaken, correct) => {    let count = 0;    for(let i = 0; i < ... Read More

Implementing Priority Sort in JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:20:26

1K+ Views

We are required to write a JavaScript function that takes in two arrays of numbers, second being smaller in size than the first.Our function should be a sorted version of the first array (say in increasing order) but put all the elements that are common in both arrays to the front.For example − If the two arrays are −const arr1 = [5, 4, 3, 2, 1]; const arr2 = [2, 3];Then the output should be −const output = [2, 3, 1, 4, 5];ExampleFollowing is the code −const arr1 = [5, 4, 3, 2, 1]; const arr2 = [2, 3]; // ... Read More

Converting a proper fraction to mixed fraction - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:19:27

350 Views

Proper FractionA proper fraction is the one that exists in the p/q form (both p and q being natural numbers)Mixed FractionSuppose we divide the numerator of a fraction (say a) with its denominator (say b), to get quotient q and remainder r.The mixed fraction form for fraction (a/b) will be −qrbAnd it is pronounced as "q wholes and r by b”.We are required to write a JavaScript function that takes in an array of exactly two numbers representing a proper fraction and our function should return an array with three numbers representing its mixed formExampleFollowing is the code −const arr ... Read More

Reversing the prime length words - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:18:30

155 Views

We are required to write a JavaScript function that takes in a string that contains strings joined by whitespaces. Our function should create a new string that has all the words from the original string and the words whose length is a prime number reversed i.e. words with length 2, 3, 5, 7, 100, etc.ExampleFollowing is the code −const str = 'His father is an engineer by profession'; // helper functions const isPrime = n => {    if (n===1){       return false;    }else if(n === 2){       return true;    }else{       ... Read More

Temperature converter using JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:16:58

399 Views

We are required to write a JavaScript function that takes in a string representing a temperature either in Celsius or in Fahrenheit.Like this −"23F", "43C", "23F"We are required to write a JavaScript function that takes in this string and converts the temperature from Celsius to Fahrenheit and Fahrenheit to Celsius.ExampleFollowing is the code −const temp1 = '37C'; const temp2 = '100F'; const tempConverter = temp => {    const degree = temp[temp.length-1];    let converted;    if(degree === "C") {       converted = (parseInt(temp) * 9 / 5 + 32).toFixed(2) + "F";    }else {       ... Read More

JavaScript - Checking for pandigital numbers

AmitDiwan
Updated on 18-Sep-2020 09:15:52

320 Views

A pandigital number is a number that contains all digits (0-9) at least once. We are required to write a JavaScript function that takes in a string representing a number. The function returns true if the number is pandigital, false otherwise.ExampleFollowing is the code to check for pandigital numbers −const numStr1 = '47458892414'; const numStr2 = '53657687691428890'; const isPandigital = numStr => {    let legend = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];    for(let i = 0; i < numStr.length; i++){       if(!legend.includes(numStr[i])){          continue;       };   ... Read More

Advertisements