Minimum Cost Polygon Triangulation

Samual Sam
Updated on 17-Jun-2020 06:56:14

876 Views

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

Minimum Initial Points to Reach Destination

Samual Sam
Updated on 17-Jun-2020 06:54:13

1K+ Views

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

Get Number of Days in a Specified Month using JavaScript

Nishtha Thakur
Updated on 17-Jun-2020 06:47:45

1K+ Views

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

Format a Rounded Number to N Decimals Using JavaScript

Anvi Jain
Updated on 17-Jun-2020 06:45:36

467 Views

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

Convert String with Scientific Notation to JavaScript Number Format

Jennifer Nicholas
Updated on 17-Jun-2020 06:44:36

1K+ Views

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

Test if a JavaScript Cookie has Expired

mkotla
Updated on 17-Jun-2020 06:43:19

3K+ Views

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...*/ );

Count Character Occurrences in a String using JavaScript

varun
Updated on 17-Jun-2020 06:39:27

812 Views

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=    

Best Way to Initialize a JavaScript Number

Prabhas
Updated on 17-Jun-2020 06:38:06

751 Views

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);          

Convert Number to String in JavaScript

vanithasree
Updated on 17-Jun-2020 06:37:14

235 Views

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

Get Decimal Portion of a Number with JavaScript

mkotla
Updated on 17-Jun-2020 06:35:57

3K+ Views

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

Advertisements