Found 9318 Articles for Object Oriented Programming

Getting equal or greater than number from the list of numbers in JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:25:50

666 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return an array of all the elements from the input array that are greater than or equal to the number taken as the second argument.ExampleFollowing is the code −const arr = [56, 34, 2, 7, 76, 4, 45, 3, 3, 34, 23, 2, 56, 5]; const threshold = 40; const findGreater = (arr, num) => {    const res = [];    for(let i = 0; i < arr.length; i++){ ... Read More

Building a Map from 2 arrays of values and keys in JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:24:32

5K+ Views

Suppose, we have two arrays −const keys = [0, 4, 2, 3, 1]; const values = ["first", "second", "third", "fourth", "fifth"];We are required to write a JavaScript function that takes in the keys and the values array and maps the values to the corresponding keys. The output should be −const map = {    0 => 'first',    4 => 'second',    2 => 'third',    3 => 'fourth',    1 => 'fifth' };ExampleFollowing is the code −const keys = [0, 4, 2, 3, 1]; const values = ["first", "second", "third", "fourth", "fifth"]; const buildMap = (keys, values) => { ... Read More

Remove array duplicates by property - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:23:13

156 Views

Suppose, we have an array of objects like this −const arr = [{name: "Jack", age: "14"}, {name: "bob", age: "14"}, {name: "sue", age: "21"}, {name: "Jill", age: "16"}, {name: "Jack", age: "21"}];We are required to write a JavaScript function that takes in one such array and removes all the objects that have duplicate values for the name.Therefore, for the above array, the output should be −const arr = [{name: "Jack", age: "14"}, {name: "bob", age: "14"}, {name: "sue", age: "21"}, {name: "Jill", age: "16"}];ExampleFollowing is the code −const arr = [    {name: "Jack", age: "14"},    {name: "bob", age: ... Read More

JavaScript - Convert an array to key value pair

AmitDiwan
Updated on 30-Sep-2020 13:21:57

6K+ Views

Suppose we have an array like this −const arr = [ {"name": "Rahul", "score": 89},    {"name": "Vivek", "score": 88},    {"name": "Rakesh", "score": 75},    {"name": "Sourav", "score": 82},    {"name": "Gautam", "score": 91},    {"name": "Sunil", "score": 79}, ];We are required to write a JavaScript function that takes in one such array and constructs an object where name value is the key and the score value is their value.Use the Array.prototype.reduce() method to construct an object from the array.ExampleFollowing is the code −const arr = [    {"name": "Rahul", "score": 89},    {"name": "Vivek", "score": 88},    {"name": ... Read More

JavaScript - How to pick random elements from an array?

AmitDiwan
Updated on 30-Sep-2020 13:20:15

2K+ Views

Suppose, we have an array of literals that contains no duplicate elements like this −const arr = [2, 5, 4, 45, 32, 46, 78, 87, 98, 56, 23, 12];We are required to write a JavaScript function that takes in an array of unique literals and a number n.The function should return an array of n elements all chosen randomly from the input array and no element should appear more than once in the output array.ExampleFollowing is the code −const arr = [2, 5, 4, 45, 32, 46, 78, 87, 98, 56, 23, 12]; const chooseRandom = (arr, num = 1) ... Read More

Changing color randomly in JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:19:12

316 Views

We are required to write a JavaScript function, let’s say randomColor that returns a randomly generated hex color every time it is called.ExampleFollowing is the code −const randomColor = () => {    let color = '#';    for (let i = 0; i < 6; i++){       const random = Math.random();       const bit = (random * 16) | 0;       color += (bit).toString(16);    };    return color; }; console.log(randomColor()); console.log(randomColor()); console.log(randomColor()); console.log(randomColor()); console.log(randomColor()); console.log(randomColor()); console.log(randomColor());OutputThis will produce the following output in console −#762b46 #cfa0bf #a20ee1 #c2f7e0 #5d5822 #380f30 #805408

Add the slug field inside Django Model

Hafeezul Kareem
Updated on 21-Sep-2020 13:02:30

385 Views

In this tutorial, we are going to learn about the SlugField in Django.SlugFieldSlugField is a way to generate a URL using the data which we already have. You can generate URL using your title of the post or page. Let's see one detailed example.Let's say we have an article with name This is from Tutorialspoint with id = 5. Then we can have URL as www.tutorialspoint.com/posts/5/. It's difficult for the content writers to recognize the article with the previous URL. But, if you have a URL like www.tutorialspoint.com/this-isfrom-tutorialspoint, then it's easy for us to identify the piece. So, SlugField is ... Read More

Returning the highest number from object properties value – JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:37:02

584 Views

Suppose, we have an object that contains rating of a property over some criteria like this −const rating = {    "overall": 92,    "atmosphere": 93,    "cleanliness": 94,    "facilities": 89,    "staff": 94,    "security": 92,    "location": 88,    "valueForMoney": 92 }We are required to write a JavaScript function that takes in one such object and returns the key value pair that has the highest value.For example, for this very object, the output should be −const output = {    "staff": 94 };ExampleFollowing is the code −const rating = {    "overall": 92,    "atmosphere": 93,   ... Read More

Find indexes of multiple minimum value in an array in JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:35:48

493 Views

Suppose we have an array of numbers like this −const arr = [1, 2, 3, 4, 1, 7, 8, 9, 1];Suppose we want to find the index of the smallest element in the array i.e. 1 above.For this, we can simply use −const min = Math.min.apply(Math, arr); const ind = arr.indexOf(min);The above code will successfully set ind to 0, which indeed is correct.But what we want to achieve is that if there are more than one minimum elements in the array, like in the above array (three 1s), then we should return an array containing all the indices of minimum ... Read More

Writing a For Loop to Evaluate a Factorial - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:34:47

1K+ Views

We are required to write a simple JavaScript function that takes in a Number, say n and computes its factorial using a for loop and returns the factorial.For example −factorial(5) = 120, factorial(6) = 720Maintain a count and a result variable, keep multiplying the count into result, simultaneously decreasing the count by 1, until it reaches 1And then finally we return the result.ExampleFollowing is the code −const num = 14; const factorial = num => {    let res = 1;    for(let i = num; i > 1; i--){       res *= i;    };    return res; }; console.log(factorial(num));OutputThis will produce the following output in console −87178291200

Advertisements