The Array.prototype.map() function of JavaScript is used create a new array with the results of called function.The syntax is as follows −arr.map(function callback(currentValue[, index[, array]])Let us now implement the Array.prototype.map() method in JavaScript −Example Live Demo Demo Heading Click to display the abs() result... Result function display() { var arr = [1, -2, 3, 4, 5, -6, 7, 8, 9, 10]; var res = arr.map(Math.abs); document.write(res); } OutputClick the “Result” button above −Example Live Demo Demo Heading Click to round the numbers... Result ... Read More
Suppose we have a special binary tree, whose leaf nodes are connected to form a circular doubly linked list. We have to find its height. So the left pointer of the left most leaf will act as previous pointer of circular doubly linked list, and its right pointer will act as next pointer of the linked list.In this case the height finding strategy is similar to the normal binary search tree. We recursively calculate the height of the left and right subtrees of a node and assign height to the node is max of the two children + 1. But ... Read More
jQuery children() methodThe children( [selector] ) method gets a set of elements containing all of the unique immediate children of each of the matched set of elements.Here is the description of all the parameters used by this method −selector − This is an optional argument to filter out all the childrens. If not supplied then all the childrens are selected.ExampleYou can try to run the following code to learn how to work with jQuery children() method:Live Demo jQuery children() method $(document).ready(function(){ ... Read More
The jQuery map function translates a set of elements in the jQuery object into another set of values in a jQuery array which may, or may not contain elements. The grep() function is used to find an element of an array. The difference is we use $.grep to filter an array and $.map to apply a function to each item in the array.jQuery map functionThe map method translates a set of elements in the jQuery object into another set of values in a jQuery array which may, or may not contain elements.The following are the parameters of jQuery.map() method:callback − ... Read More
Suppose we have three coordinates which are midpoint of sides of the triangle. We have to find the coordinates of the triangle. So if the inputs are like (5, 3), (4, 4), (5, 5), then output will be (4, 2), (4, 6), (6, 4).To solve this, we have to solve for X-coordinates and Y-coordinates separately. For X coordinate of vertices, let them be x1, x2, x3. Then, X-coordinate of middle points will be (x1 + x2)/2, (x2 + x3)/2, (x3 + x1)/2. If we observe the sum of these three expressions is equal to sum of X-coordinates. Now, we have ... Read More
Suppose we have an array A with n elements. And elements are sorted. We have to find the closest value to the given integer. The array may contain duplicate values and negative numbers. So if the array is like [2, 5, 6, 7, 8, 8, 9] and the target number is 4, then closest element is 5.We can solve this by traversing through the given array and keep track of absolute difference of current element with every element. Finally return the element that has minimum absolute difference.Example Live Demo#include #include using namespace std; int getNearest(int x, int y, int target) { ... Read More
To remove all child nodes from a parent in jQuery, use the empty() method.ExampleThe jQuery empty() method removes all child nodes of the set of matched elements from the DOM.Live Demo $(document).ready(function(){ $("button").click(function() { $("#demo").empty(); }); }); India US UK Remove all child nodes
To get the height of an element using jQuery, use the height() method.ExampleYou can try to run the following code to get the height of an element using jQuery:Live Demo $(document).ready(function(){ $("button").click(function(){ alert("Height of element: " + $("div").height()); }); }); Get Height of element
To set the width and height of an element using jQuery, use the width() and height() in jQuery.Width of an elementExampleYou can try to run the following code to learn how to set the width of an element in jQuery:Live Demo $(document).ready(function(){ $("#button1").click(function(){ $("div").width(300); }); $("#button2").click(function(){ $("div").width(400); }); }); Width of div: 300 Width of div: 400 Height of an elementExampleYou can try to run the following code to learn how to set the ... Read More
jQuery uses CSS selector to select elements using CSS. Let us see an example to return a style property on the first matched element. The css( name ) method returns a style property on the first matched element.Here is the description of all parameter used by this method −name − The name of the property to access.ExampleYou can try to run the following code to learn how jQuery selects elements with CSS:Live Demo jQuery Example $(document).ready(function() { ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP