Found 9150 Articles for Object Oriented Programming

JavaScript function to take a number n and generate an array with first n prime numbers

Shriansh Kumar
Updated on 30-Sep-2024 16:01:16

2K+ Views

We are required to write a JavaScript function that takes in a number n and returns an array that contains first n prime numbers. We know that prime numbers are those numbers that are only divisible by 1 and themselves like 2, 3, 19, 37, 73 etc. Let's understand the problem with an example − Input: n = 6; Output: prime_numbers = [ 2, 3, 5, 7, 11, 13 ] Using Iteration We will first write a function that checks whether a given number is prime or not and then run a loop till the given number n ... Read More

How to compare two arrays to see how many same elements they have in JavaScript?

AmitDiwan
Updated on 28-Aug-2020 13:57:42

406 Views

Let’s say, we have two arrays, one contains the correct answer strings of some questions and one contains the answers attempted by a candidate, but somehow the arrays got shuffled and now they don’t have answers in corresponding order. But we can be sure that no two questions had the same answers.Our job now is to write a function that takes these two arrays, checks them for common elements and finds all the common elements between them and then calculates the marks percentage of the candidate based on the count of common answers.Let’s write the code for this function −Exampleconst ... Read More

Find unique and biggest string values from an array in JavaScript

AmitDiwan
Updated on 28-Aug-2020 13:56:28

211 Views

Let’s say, we have an array of objects like this −Exampleconst arr = [    {text:'use'},    {text: 'secur'},    {text: 'form'},    {text: 'user'},    {text: 'users'},    {text: 'form'},    {text: 'secur'},    {text: 'sec'},    {text: 'users'},    {text: 'secu'},    {text: 'secur'},    {text: 'for'},    {text: 'form'} ]Our job is to write a function that takes in this array and a number n and the function should return an array of n objects which have the longest string value for the text key and all the objects should have a unique value for the text ... Read More

Get n numbers from array starting from given point JavaScript

AmitDiwan
Updated on 28-Aug-2020 13:54:33

271 Views

We have to write an array function (Array.prototype.get()) that takes in three arguments first, a number n, second is also a number, say m, (m this.length-1){       return false;    };    const res = [];    for(let i = ind, j = 0; j < num; i += amount, j++){       if(i > this.length-1){          i = i % this.length;       };       if(i < 0){          i = this.length-1;       };       res.push(this[i]);    };    return res; }; console.log(arr.get(4, 6, 'right')); console.log(arr.get(9, 6, 'left'));OutputThe output in the console will be −[ 6, 7, 0, 1 ] [    6, 5, 4, 3, 2,    1, 0, 7, 6 ]

How to force JavaScript to do math instead of putting two strings together?

AmitDiwan
Updated on 28-Aug-2020 13:52:13

413 Views

Let’s say, we have an array of strings, basically it is an array of number strings like this −const arr = ['3', '3', '55', '23', '67', '43', '12', '67', '87', '12'];We are required to write a JavaScript function that takes in one such array and returns the sum of all elements of this array instead of concatenating the string to one another.Let’s write the code for this function −Exampleconst arr = ['3', '3', '55', '23', '67', '43', '12', '67', '87', '12']; const sumString = arr => {    const num = arr.reduce((acc, val) => {       const sum ... Read More

Solve the Sherlock and Array problem in JavaScript

AmitDiwan
Updated on 28-Aug-2020 13:51:03

667 Views

Watson gives Sherlock an array A of length N. Then he asks him to determine if there exists an element in the array such that the sum of the elements on its left is equal to the sum of the elements on its right.We have to write this function, it should take in an array of Numbers, and any such number exists in the array, it should return its index, otherwise it should return -1. So, let’s write the code for this function −Exampleconst arr = [1, 2, 3, 4, 5, 7, 3]; const arr2 = [4, 6, 3, 4, ... Read More

Compare two objects in JavaScript and return a number between 0 and 100 representing the percentage of similarity

AmitDiwan
Updated on 28-Aug-2020 13:49:28

554 Views

Let’s say, we have two objects like these −const a = {    Make: "Apple",    Model: "iPad",    hasScreen: "yes",    Review: "Great product!", }; const b = {    Make: "Apple",    Model: "iPad",    waterResistant: false };We are required to write a function that counts the number of common properties in the objects (by common property we mean having both key and value same) and returns a number between 0 and 100 (both inclusive) that represents the percentage of similarity between the objects. Like if no key/value pair matches it will be 0, if all matches it ... Read More

Get greatest repetitive item in array JavaScript

AmitDiwan
Updated on 28-Aug-2020 13:47:41

138 Views

We have an array of Number / String literals that contains some values (some are repetitive as well). Our job is to write a function that returns the element from the array which appears for the greatest number of times in the array.For example − if the input array is −const input = ['a', 'v', 'k', 'f', 'a', 'f', 's', 'd', 'd', 'f', 'a', 'j', 'a'];Then the output should be −'a'because 'a' gets repeated for the maximum number of timesTherefore, let’s write the code for this. We will use a Map() to keep track of all the elements we encounter ... Read More

Array sum: Comparing recursion vs for loop vs ES6 methods in JavaScript

AmitDiwan
Updated on 28-Aug-2020 13:45:18

265 Views

Let’s say, we have an array with a huge number of Number entries and are required to compare the time recursion takes versus the time a simple loop takes versus the time ES6 function takes in summing all the entries of the array i.e. recursion vs for loop vs ES6 function.Here, to simulate a large array we will iterate over a relatively smaller array for a large number of times (of the order of 10000000). Our main aim is just to have a rough ratio of the time each method takes in summing the array.Part 1: Recursive Approachconst recursiveSum = ... Read More

Fetch Second minimum element from an array without sorting JavaScript

AmitDiwan
Updated on 28-Aug-2020 13:42:17

480 Views

We have an array of Numbers, and we are required to write a function that returns the second smallest value from the array.For example − if the array is −const arr = [67, 87, 56, 8, 56, 78, 54, 67, 98, 56, 54];Then the output should be the following −54because 54 is the smallest value after 8Exampleconst arr = [67, 87, 56, 8, 56, 78, 54, 67, 98, 56, 54]; const minimumIndex = arr => {    return arr.indexOf(Math.min(...arr)); }; const secondMinimum = arr => {    const copy = arr.slice();    copy.splice(minimumIndex(copy), 1);    return copy[minimumIndex(copy)]; }; console.log(secondMinimum(arr));OutputThe output in the console will be −54

Advertisements