When nonintersecting diagonals are forming a triangle in a polygon, it is called the triangulation. Our task is to find a minimum cost of triangulation.The cost of triangulation is the sum of the weights of its component triangles. We can find the weight of each triangle by adding their sides, in other words, the weight is the perimeter of the triangle.Input and OutputInput: The points of a polygon. {(0, 0), (1, 0), (2, 1), (1, 2), (0, 2)} Output: The total cost of the triangulation. Here the cost of the triangulation is 15.3006.AlgorithmminCost(polygon, n)Here cost() will be used to calculate ... Read More
To start from the top-left corner of a given grid, one has to reach the bottom-right corner. Each cell in the grid contains a number, the number may positive or negative. When the person reaches a cell (i, j) the number of tokens he has, may be increased or decreased along with the values of that cell. We have to find the minimum number of initial tokens are required to complete the journey.There are some rules −We can either move to the right or to the bottom.We cannot move to a cell (i, j) if our total token is less ... Read More
To get numbers of days in a month, use the getDate() method and get the days.ExampleYou can try to run the following code to get a number of days in a month −Live Demo var days = function(month,year) { return new Date(year, month, 0).getDate(); }; document.write("Days in July: "+days(7, 2012)); // July month document.write("Days in September: "+days(9, 2012)); // September Month OutputDays in July: 31 Days in September: 30
Use the toFixed() method to format a rounded number to N decimals. The toFixed() method formats a number with a specific number of digits to the right of the decimal.ExampleYou can try to run the following code to form a rounded number to N decimals −Live Demo JavaScript toFixed() Method var num = 15; document.write("num.toFixed() is : " + num.toFixed()); document.write(""); document.write("num.toFixed() is : " + num.toFixed(2)); document.write(""); document.write("num.toFixed() is : " + num.toFixed(1)); document.write(""); Outputnum.toFixed() is : 15 num.toFixed() is : 15.00 num.toFixed() is : 15.0
To convert a string with Scientific Notation, use the Number function. Pass the value to this function.ExampleYou can try to run the following code to convert a string to correct number format −Live Demo document.write("String with Scientific Notation converted below:"); document.write(Number("7.53245683E7")); OutputString with Scientific Notation converted below: 75324568.3
To test if a cookie has expired in JavaScript, add the following condition and check −if( !($.cookie('myCookie') === null) ) { // Do something (cookie exists) } else { // Do something (cookie expired) }You can also check it using a single line code −checkCookie = $.cookie('myCookie') === null ? ( /*Condition TRUE...*/ ) : ( /*Condition FALSE...*/ );
To count the number of occurrences of a character in a string, try to run the following code −ExampleLive Demo function displayCount() { setTimeout(function() { var str = document.getElementById('str1').value; var letter = document.getElementById('letter1').value; letter = letter[letter.length-1]; var lCount; for (var i = lCount = 0; i < str.length; lCount += (str[i++] == letter)); document.querySelector('p').innerText = lCount; return lCount; }, 50); } Enter String: Enter Letter: Click for Result result=
The best way to initialize a number in JavaScript is to initialize variables while declaring them. Through this, you can easily avoid undefined values.ExampleYou can try to run the following code to initialize a number −Live Demo var deptNum = 20, id = 005; document.write("Department number: "+deptNum);
The best way to convert a number to a string is using the toString() method, with different bases. The method has an optional parameter “radix”, which is used as a base (2, 8, 16) for a numeric value.ExampleYou can try to run the following code to convert a number to a string −Live Demo var num = 10; var val1 = num.toString(); var val2 = num.toString(2); var val3 = num.toString(8); var val4 = num.toString(16); document.write(val1 + "" + val2 + "" + val3 + "" + val4); Output10 1010 12 a
With JavaScript, use the % operator to get the decimal portion of a number.ExampleYou can try to run the following to get decimal portion −Live Demo var num1 = 5.3; var num2 = 4.2; var num3 = 8.6; document.write(num1 % 1); document.write(""+num2 % 1); document.write(""+num3 % 1); Output0.2999999999999998 0.20000000000000018 0.5999999999999996
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP